繁体   English   中英

使用html服务使用google apps脚本上传文件

[英]Upload file with google apps script using html service

我想使用GAS的HtmlService上传文件,但它不起作用。 我在本主题中找到了使用HtmlService使用Google Apps脚本上传文件的信息,但它不符合我的问题。 这是一个描述我的问题的例子:

Code.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>

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

code.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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM