如何将文件上传到google drive?我想使用google app script - htmlservice创建一个web应用程序。我不知道如何将html中的表单指向现有的google应用程序脚本。我很难在google文档中找到一个正确的例子。
我发现了成百上千个使用UI的例子,但根据https://developers.google.com/apps-script/sunset的说法,它很快就会被弃用。提前谢谢你!Janusz
<html>
<body>
<form>
<input type="file"/>
<input type="button">
</form>
</body>
</html>
脚本
function doGet() {
return HtmlService.createHtmlOutputFromFile('myPage');
}
function fileUploadTest()
{
var fileBlob = e.parameter.upload;
var adoc = DocsList.createFile(fileBlob);
return adoc.getUrl();
}
发布于 2013-04-03 14:51:52
让按钮使用google.script.run运行服务器端函数,并将整个表单作为唯一的参数传入。(在按钮的onClick中,'this‘是按钮,所以'this.parentNode’是表单。)确保为文件输入指定一个名称。
<html>
<body>
<form>
<input type="file" name="theFile">
<input type="hidden" name="anExample">
<input type="button" onclick="google.script.run.serverFunc(this.parentNode)">
</form>
</body>
</html>
在服务器上,让表单处理函数接受一个参数-表单本身。来自客户端代码的超文本标记语言表单将被转换为等效的JavaScript对象,其中除了将是blobs的文件之外,所有命名字段都是字符串属性。
function doGet() {
return HtmlService.createHtmlOutputFromFile('myPage');
}
function serverFunc(theForm) {
var anExampleText = theForm.anExample; // This is a string
var fileBlob = theForm.theFile; // This is a Blob.
var adoc = DocsList.createFile(fileBlob);
return adoc.getUrl();
}
如果您实际上想要使用正在生成和返回URL,请确保向google.script调用添加一个成功处理程序。您可以这样修改它:
// Defined somewhere before the form
function handler(url) {
// Do something with the url.
}
<input type="button" onclick=
"google.script.run.withSuccessHandler(handler).serverFunc(this.parentNode)">
发布于 2013-03-28 12:37:03
尝试:返回HtmlService.createTemplateFromFile('myPage').evaluate();More:html service reference
发布于 2013-03-28 15:15:03
我找到了我的问题的答案。
Submit a Form using Google App Script's HtmlService
Google App脚本链接中的代码如下:
function doGet(e) {
var template = HtmlService.createTemplateFromFile('Form.html');
template.action = ScriptApp.getService().getUrl();
return template.evaluate();
}
function doPost(e) {
var template = HtmlService.createTemplateFromFile('Thanks.html');
template.name = e.parameter.name;
template.comment = e.parameter.comment;
template.screenshot = e.parameter.screenshot;
return template.evaluate();
}
https://script.google.com/d/1i65oG_ymE1lreHtB6WBGaPHi3oLD_-wPd5Ter1nsN7maFAWgUA9DbE4C/edit
谢谢!
https://stackoverflow.com/questions/15670392
复制相似问题