Google Script for Uploading File Not Working

2019-08-04 12:56发布

问题:

I wrote a script for uploading files to my Google Drive using the Google Script. I deployed it as a WebApp but it's not working and I don't know why. The button just gets stuck on "Subiendo..." and never changes anything inside my Drive. I'm sure I gave the script all the permissions and I already tried to see what's happening on my own without any sucess. Can you maybe help me find what should I do? I post the code here:

Code.gs

function doGet() {
    return    HtmlService.createHtmlOutputFromFile('main').setSandboxMode(HtmlService.SandboxMode.IFRAME);
}

function serverFunc(theForm) {
    try{
        var folder_name = "publicaciones";
        var folder,folders = DriveApp.getFoldersByName(folder_name);

        if(folders.hasNext()){
             folder = folders.next();
        }else{
             folder = DriveApp.createFolder(folder_name);
        }
        Logger.log("TheForm", theForm == null);
        var blob = theForm.theFile;
        Logger.log("Blob!", blob == null);
        var file = folder.createFile(blob);
        Logger.log("File:", file.getName());
        return "Archivo subido exitosamente: " + file.getUrl();

     } catch(error){
        Logger.log("error: ", error.toString());
        return error.toString();    
  }
}

** main.html **

<div>
<form id="myForm">
   <input type="file" name="theFile">
   <input type="hidden" name="anExample">
   <br>
   <input type="button" value="Subir Archivo" onclick="this.value='Subiendo...';
                                                       google.script.run.withSuccessHandler(fileUploaded).serverFunc(this.parentNode);
                                                       return false;">
</form>
</div>
<div id="output">
</div>

<script>
    function fileUploaded(status) {
        document.getElementById('myForm').style.display = 'none';
        document.getElementById('output').innerHTML = status;
    }
</script>

I'd appreaciate any help or pointers you can give me. Thanks a lot!

回答1:

Looks like there is a bug currently that Google is working on. I found a possible fix:

Change your input tag to this:

<input type="button" value="Subir Archivo" onclick="callServerCode()"/>

Add a function to the <script> tag:

<script>

  function callServerCode() {
    var formToSend = document.getElementById('myForm');

    google.script.run
      .withSuccessHandler(fileUploaded)
      .serverFunc(formToSend);
  };

    function fileUploaded(status) {
        document.getElementById('myForm').style.display = 'none';
        document.getElementById('output').innerHTML = status;
    }

</script>

Note that inside the new function, it gets the form using var formToSend = document.getElementById('myForm');

And change IFRAME to NATIVE.



回答2:

It's a doc issue needs to fix with Google when using SandBoxMode=IFRAME currently. See here. I've tested it works by swapping IFRAME to NATIVE. You can simply do it:

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


回答3:

Google has turned of NATIVE for SandBoxMode. It is now set to IFRAME only. See here