Skip to content

Instantly share code, notes, and snippets.

@leoherzog
Last active December 19, 2017 18:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leoherzog/98cce497cd7e13b8b74c291f116cc966 to your computer and use it in GitHub Desktop.
Save leoherzog/98cce497cd7e13b8b74c291f116cc966 to your computer and use it in GitHub Desktop.
Google Apps Script: Upload a File to Google Drive via HTTP POST
// https://developers.google.com/apps-script/guides/web
//
function doPost(e) {
if (!e.parameter.filename) {
return HtmlService.createHtmlOutput("Don't forget a filename parameter in the URL!");
}
if (!e.postData.contents) {
return HtmlService.createHtmlOutput("No POST data received :(");
}
// create a new folder in Google Drive
// DriveApp.createFile(name, content, mimeType)
var drivefile = DriveApp.createFile(e.parameter.filename, e.postData.contents, e.postData.type);
// drive files aren't actually "in" folders. they're treated like gmail messages and labels.
// in gmail, one message can have multiple labels.
// in drive, one file can have multiple folders.
// more explanation here: https://www.labnol.org/internet/add-files-multiple-drive-folders/28715/
// here, i remove the file from the root folder and add it to a custom folder specified.
DriveApp.getRootFolder().removeFile(drivefile);
var folder = DriveApp.getFolderById('1vZOE5wXXEq5DV4ljfY7-7vctASDf786asdf').addFile(drivefile);
return HtmlService.createHtmlOutput("File added successfully to the " + folder.getName() + " folder!");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment