gpt4 book ai didi

javascript - 使用 html 服务通过 google apps 脚本上传文件

转载 作者:行者123 更新时间:2023-12-02 17:47:17 26 4
gpt4 key购买 nike

我想使用GAS的HtmlService上传文件,但不起作用。我在这个主题Uploading file using Google Apps Script using HtmlService找到了信息但它不符合我的问题。这是描述我的问题的示例:

代码.gs:

    function doGet() {

return HtmlService.createTemplateFromFile("index").evaluate();

}

//For some reasons, this function has to be in code.gs
funcion getHTML() {

var html = "<form id='myform'>"+
"<input name='myfile'>"+
"</form>"+
"<div data-formname='myform'> Submit form </div>";

return html;

}

function uploadFile(file) {

DriveApp.createFile(file); *//Doesn't work :/*

}

Javascript:

    $(document).on("click","div", function(e) {

var idform = $(this).data("formname");

if (idform)
sendForm(idform);


});

function trigger() {

google.script.run.withsuccesshandler(setHTML).getHTML();

}

function setHTML(html) {

$("#myHTML").html(html);

}

function sendForm(idForm) {

var formElements = document.getElementById("idForm").elements;
var form = {};

for (var key in formElements) form[key] = formElements[key];

google.script.run.withsuccesshandler().uploadFile(form["myfile"]);

}

index.html

<body onload="trigger()">

<div id='myHTML'></div>

</body>

最佳答案

您的代码似乎有点过于复杂...尝试这个简单的代码

代码.gs

function doGet() {
return HtmlService.createHtmlOutputFromFile('index');
}

function serverFunc(theForm) {
var fileBlob = theForm.theFile; // This is a Blob.
var adoc = DocsList.createFile(fileBlob);
return adoc.getUrl();
}

index.html

<div>
<script>
function cliHandler(e){
document.getElementById('button').value = "Document is downloadable at url "+e
}
</script>

<form>
<input type="file" name="theFile">
<input type="button" value="UpLoad" id="button" onclick="google.script.run.withSuccessHandler(cliHandler).serverFunc(this.parentNode)">
</form>
</div>

关于javascript - 使用 html 服务通过 google apps 脚本上传文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21648766/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com