invitation india
UnexpectedWeb
Connect

Popular Technology Blog where you'll find some interesting things around the web, that you never knew existed.

link Link copied

Upload Files to Google Drive with Google Apps Script - WebApp

Share on: link Link copied
Learn how to create a simple google App Script web app to upload files to google drive folder.

The following example demonstrates how to save a file to a Google Drive using Google App Script - Web App.

1. Open Google App Script
2. Paste Following Code in Code.gs

function doGet() {
  return HtmlService.createHtmlOutputFromFile('Form.html');
}

function upload(e) { 
    
  try {
    
     // Folder ID of destination folder
     var destination_id = '16-9aR67D6HlSnQR9Nf_1CuV-Qz1CzB9C';
    
    // Converting files to image/jpeg.
    // You can use 'application/pdf', 'image/bmp', 'image/gif', 'image/jpeg' and 'image/png'.   
    var contentType = 'image/jpeg'; 
    
    var img = e.imageFile;

    var destination = DriveApp.getFolderById(destination_id);
    // var img = img.getAs(contentType); // for: CommConverting files and save as image/jpeg.
    destination.createFile(img);
    
    return "File Uploaded Successfully!";
    
  } catch (m) {
  return m.toString();
  }
  
}
3. Creae new Html File Form.html. File > New > Html file and Enter new file name "Form".

4. Paste Following Code in Form.html


<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
   <script>
   function updateUrl(msg) {
    var div = document.getElementById('output');
    div.innerHTML = '<span>' + msg +'</span>';
    }
    </script>
    
    <form>
      <input type="file" name="imageFile">
      <input type="button" value="Upload File" onclick="google.script.run.withSuccessHandler(updateUrl).upload(this.parentNode)">
    </form><br/><br/>
   <div id="output"></div>
  </body>
</html>

4. To publish a script as a web app, Publish > Deploy as web app.
5. Under Project version, enter the version like: 1,2,3 etc.
6. Click Deploy.
7. To test your web appPublish > Deploy from manifest...
8. Click on web app URL.


Googel App Script Web app URL



3 comments:

  1. Hey, this used to work just fine, but for some reason quit recently. Can't see any issues or changes. The "upload" button from the form.html just never appears to do anything. As if it's not even launching the script.

    Did google change something maybe?

    ReplyDelete
    Replies
    1. Had the same problem. Seems as if Google moved to a new runtime Version (V8). This script only works with the now deprecated ES5.

      To get it running again using ES5:
      Select "View > Show project manifest" and change "runtimeVersion" from "v8" to "DEPRECATED_ES5"

      Hope this helps

      Cheers

      Delete

Powered by Blogger.