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 |
Upload Files to Google Drive with Google Apps Script - WebApp
Reviewed by abonzertv
on
November 28, 2017
Rating: 5
