2014-02-08 25 views
0

我想使用GAS的HtmlService上傳文件,但它不起作用。 我在這個主題中發現了信息Uploading file using Google Apps Script using HtmlService,但它不符合我的問題。 這裏是描述我的問題的例子:使用html服務將文件上傳到谷歌應用程序腳本

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> 
+0

下面的例子可能是有用的:[窗體]( https://developers.google.com/apps-script/guides/html/communication#forms) – wchiquito

回答

2

您的代碼似乎有點過於複雜...試試這個簡單的代碼

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> 
相關問題