Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save xtman/50a8c5f06532d9878d02a12a2a8d4555 to your computer and use it in GitHub Desktop.
Save xtman/50a8c5f06532d9878d02a12a2a8d4555 to your computer and use it in GitHub Desktop.
Google App Script: Upload Multiple Files to Google Drive (Code.gs)
function doGet() {
return HtmlService.createHtmlOutputFromFile('Form').setSandboxMode(
HtmlService.SandboxMode.IFRAME);
}
function createFolder(parentFolderId, folderName) {
try {
var parentFolder = DriveApp.getFolderById(parentFolderId);
var folders = parentFolder.getFoldersByName(folderName);
var folder;
if (folders.hasNext()) {
folder = folders.next();
} else {
folder = parentFolder.createFolder(folderName);
}
return {
'folderId' : folder.getId()
}
} catch (e) {
return {
'error' : e.toString()
}
}
}
function uploadFile(base64Data, fileName, folderId) {
try {
var splitBase = base64Data.split(','), type = splitBase[0].split(';')[0]
.replace('data:', '');
var byteCharacters = Utilities.base64Decode(splitBase[1]);
var ss = Utilities.newBlob(byteCharacters, type);
ss.setName(fileName);
var folder = DriveApp.getFolderById(folderId);
var files = folder.getFilesByName(fileName);
var file;
while (files.hasNext()) {
// delete existing files with the same name.
file = files.next();
folder.removeFile(file);
}
file = folder.createFile(ss);
return {
'folderId' : folderId,
'fileName' : file.getName()
};
} catch (e) {
return {
'error' : e.toString()
};
}
}
@osjinc
Copy link

osjinc commented Feb 26, 2017

Hi. you did not try to integrate with google spredsheets? To add the name, email and a link to the file?
SpreadsheetApp.openById(SHEET_ID).getSheets()[0].appendRow([new Date(), form['applicantName'], form['applicantEmail'], file.getUrl(), file2.getUrl()]);

@alete89
Copy link

alete89 commented May 19, 2017

Thanks for this!

@adit-s
Copy link

adit-s commented Aug 31, 2017

I was looking for a script which allows a user to upload multiple files and found this. It's exactly what I'm looking for, but it's not working for me. The html form correctly selects the files and uploads them, but my theory is the folders (based on username and email) are not being created properly.
I wonder if something's changed with Google Drive recently which prevents this script from working.
Are you able to test it again and confirm that it still works?

Thanks in advance,
Adit

@WSFORERO
Copy link

Buenos dias realizando pruebas con el código me carga la información pero se está perdiendo en el momento de almacenarla, ¿algún cambio que se deba realizar? , agradecería su ayuda

@nguyenthephuc
Copy link

I run this script and it upload only 35 files, no more. It was Uncaught.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment