【问题标题】:Upload file with google apps script using html service使用 html 服务上传带有谷歌应用脚​​本的文件
【发布时间】:2014-03-06 02:31:54
【问题描述】:

我想使用 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>

【问题讨论】:

  • 以下示例可能有用:Forms

标签: javascript google-apps-script


【解决方案1】:

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

代码.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>

【讨论】:

    猜你喜欢
    • 2012-08-24
    • 2012-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多