似乎这样应该会更容易。但诚然,我不理解blob。
function doGet(e) {
var app = UiApp.createApplication();
var panel = app.createVerticalPanel().setId('panel');
var fileUpload = app.createFileUpload().setName('theFile').setId('theFile');
var handler = app.createServerChangeHandler('uploadfile');
handler.addCallbackElement(panel);
fileUpload.addChangeHandler(handler);
panel.add(fileUpload);
app.add(panel);
return app;
}
function uploadfile(e)
{
// data returned which can be used to create a blob
// assuming mime-type to be a text file in this example
var fileBlob = Utilities.newBlob(e.parameter.thefile, "text/plain","file.txt" );
// Create a new file
var doc = DocumentApp.create('Uploaded Text File');
doc.appendParagraph(fileBlob.getDataAsString());
// Save and close the document
doc.saveAndClose();
//var doc = DocsList.createFile(fileBlob.getDataAsString());
var app = UiApp.getActiveApplication();
app.getElementById('panel').add(app.createLabel('File Uploaded successfully'));
return app;
}
当我尝试使用此Google Apps脚本代码上传文件时,我一直收到未定义的内容。我想要做的就是将文本文件上传到我的Google云端硬盘。
我该怎么做才能修复此代码?或者有更好的方法吗?
答案 0 :(得分:1)
Google Apps脚本现在有"实验"资源Advanced Google Services
。我统计了15项高级服务,包括Drive API
。
点击Resources
菜单:
您还必须访问Google Developers控制台,并在那里启用Drive Service
。
Google文档位于:Advanced Drive Services
以下是Google的一些示例代码:
function uploadFile() {
var image = UrlFetchApp.fetch('http://goo.gl/nd7zjB').getBlob();
var file = {
title: 'google_logo.png',
mimeType: 'image/png'
};
file = Drive.Files.insert(file, image);
Logger.log('ID: %s, File size (bytes): %s', file.id, file.fileSize);
}
<强> 修改 强>
我认为无法使用HTML服务将文件从用户计算机上传到Google云端硬盘,但幸运的是我又错了! :)
答案 1 :(得分:0)
要进行文件上传,您不能使用服务器处理程序。您必须使用FormPanel和Submit按钮。请参阅FileUpload widget's documentation。