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()
};
}
}
@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