Upload Files to Google Drive with Google Apps Script - WebApp
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
3. Creae new Html File Form.html. File > New > Html file and Enter new file name "Form".
4. Paste Following Code in Form.html
5. Under Project version, enter the version like: 1,2,3 etc.
6. Click Deploy.
7. To test your web app, Publish > Deploy from manifest...
8. Click on web app URL.
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();
}
}
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 app, Publish > Deploy from manifest...
8. Click on web app URL.
Googel App Script Web app URL |
Can't view image problem
ReplyDeleteHey, 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.
ReplyDeleteDid google change something maybe?
Had the same problem. Seems as if Google moved to a new runtime Version (V8). This script only works with the now deprecated ES5.
DeleteTo get it running again using ES5:
Select "View > Show project manifest" and change "runtimeVersion" from "v8" to "DEPRECATED_ES5"
Hope this helps
Cheers