我尝试使用Google脚本将用户的文件上传到我的Google云端硬盘。
我将google scipt部署为网络应用。我有两个文件:
form.html
<div id="output"></div>
<form id="myForm">
<input type="hidden" name="user_id" id="user_id" value="<?= user_id ?>">
<input type="file" name="myFile">
<input type="submit" id="upload_button" value="Upload File"
onclick="this.value='Uploading..';
google.script.run.withSuccessHandler(fileUploaded)
.uploadFiles(this.parentNode);
return false;">
</form>
<script>
function fileUploaded(status) {
document.getElementById('upload_button').value = "Upload File";
document.getElementById('output').innerHTML = status;
}
</script>
<style>
input { display:block; margin: 20px; }
</style>
server.gs
function folderExists(name) {
var folders = DriveApp.getFoldersByName(name);
return folders.hasNext();
}
function createForm(uid) {
var t = HtmlService.createTemplateFromFile('form.html');
t.user_id = uid;
return t.evaluate().setSandboxMode(HtmlService.SandboxMode.NATIVE);
}
function doGet(e) {
var uid = e.parameters.user_id;
if (uid && folderExists(uid))
return createForm(uid);
return HtmlService.createHtmlOutput('<b>Request is not valid</b>');
}
function uploadFiles(form) {
try {
var uid = form.user_id;
var folder, folders = DriveApp.getFoldersByName(uid);
if (folders.hasNext()) {
folder = folders.next();
} else {
return "Unable to upload file";
}
var blob = form.myFile;
var file = folder.createFile(blob);
return "File uploaded successfully";
} catch (error) {
return error.toString();
}
}
我尝试上传48 Mb文件并收到异常:
Exception: File <filename> exceeds the maximum file size.
Google是否有将文件推送到云端硬盘的一些限制?我使用1 TB计划。
修改
我试图使用
Drive.Files.insert
功能。它有以下限制:最大文件大小:5120GB,如下所述:https://developers.google.com/drive/v2/reference/files/insert
因此,在这种情况下,我也无法将文件上传到我的Google云端硬盘并收到此信息:
Exception: Empty response