这是How to Use Advanced Drive Service to Upload Files的延续。
我的Webapp包含数据文件的上传表单,然后存储在Google云端硬盘中。 (下面的代码段中的完整代码。) 我遇到以下代码行的问题:
var file = Drive.Files.insert(resource, mediaData); // create file using Drive API
try {
//Get root folder and pull all existing folders, plus setup variables pulled from form
var dropbox = form.Country;
var timeStamp = new Date();
//Set file name slightly differently for Weekly Member Report (do not want to overright based on name just keep each extract so add timestamp to name)
if (form.reportType == "Member Weekly"){
var filename = form.reportType + timeStamp + ".xls";
}
else
{
var filename = form.reportType+".xls";
}
var rootfolder = DriveApp.getFolderById("0Byvtwn42HsoxfnVoSjB2NWprYnRiQ2VWUDZEendNOWwwM1FOZk1EVnJOU3BxQXhwU0pDSE0");
//Note root folder is Live Uploads Folder in Flatworld App folder structure
var folder, folders = rootfolder.getFoldersByName(dropbox);
//Check if folder exists and if not create
if (folders.hasNext()) {
folder = folders.next();
} else {
folder = rootfolder.createFolder(dropbox);
}
var FolderURL = folder.getUrl(); // Retain URL of folder for final end message to user
//Check if file already exists and delete if it does
var file, files = folder.getFilesByName(filename);
while( files.hasNext()){
file = files.next();
file.setTrashed(true);
}
//New Code from Stackover Flow:
//Upload file and set various properties
var mediaData = form.myFile;
var resource = {
title: filename,
mimetype: 'application/vnd.ms-excel',
description: "Uploaded via BNI Upload Form by " + form.myName + " on: " + timeStamp
};
var file = Drive.Files.insert(resource, mediaData); // create file using Drive API
var fileId = file.id;
var DriveAppFile = DriveApp.getFileById(fileId); // retrieve file in DriveApp scope.
var FileURL = DriveAppFile.getUrl(); //Retain URL of file for final end message to user
DriveApp.removeFile(DriveAppFile); // remove new file from Users root My Drive
folder.addFile(DriveAppFile); // puts file in selected folder
//End of New code from Stackover Flow
//Success message displayed to user
return "Thanks! File uploaded successfully to: <br><br><b>Folder Location:</b> " + FolderURL + "<br>" + "<b>File:</b> " + FileURL + ". <br><br>For any queries please email user@example.com copying the URLs displayed here in your email. You can close this browser window now or use the link below to upload another file.<br><br>";
} catch (error) {
//Catch error return it to user and email with error details
&#13;
抛出错误消息&#34;空响应&#34;在我们尝试上传大文件时,在上面的代码行(15MB)你有什么建议吗?这完全在文件插入限制5120GB内,并且代码在较小的文件上正常工作。
我现在试图添加一个循环函数来尝试上传几次,但仍然遗憾地抛出同样的错误:
//setup function that will return null if file is not uploaded correctly
function createDriveFile(resource_f, mediaData_f){
try{
var file = Drive.Files.insert(resource_f, mediaData_f); // create file using Drive API
return file;
} catch (e) {return null;}
}
//try upload and loop if null
var maxTries = 3;
var tries = 0;
do {
tries = tries + 1;
if (tries > 0) {
var file = createDriveFile(resource, mediaData);
Logger.log("I'm trying to upload, try number: " + tries);
}
} while ((file == null) && (tries < maxTries));
if (file == null) {
var file = Drive.Files.insert(resource, mediaData); // try one laste time to create file using Drive API - outside loop function so if error is thrown script stops
}
&#13;
错误似乎只发生在较大的文件上,即使我们减少了解决错误的同一文件的大小,因此我们需要调整上传过程以考虑更大的文件。是否有Google Apps脚本相当于制作API upload request resumable?
答案 0 :(得分:1)
您的文件大小是决定因素。参考文档表明,此处使用的简单上传方法仅适用于5MB。
https://developers.google.com/drive/web/manage-uploads
您的评论似乎证实了这就是您正在发生的事情。
正如您所暗示的那样,请使用可恢复的上传方法。使用uploadType: resumable
参数标记 - API docs on the insert method描述了如何。
答案 1 :(得分:0)
您还可以检查文件的命名,对我来说,我的名称中有一个斜线,这就是为什么它不上传的原因。因此,在上传之前请删除所有特殊字符,它应该可以工作。