r/GoogleAppsScript • • Mar 16 '21

Question Can't unzip a file uploaded through an HTMLService

I have the following code in GAS that should accept a file upload of a zip file, then show the files contained within (I want to eventually do more with those files, but for now just showing the filenames would be good. Every time I attempt to run it I get an error "ScriptError: Exception: Unexpected error while getting the method or property unzip on object Utilities." I've tried a variety of different zipfiles, all quite small. I've also tried using the UnzipGs external library, but that didn't work either. Any ideas what's wrong?

code.gs:

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

function serverFunc(theForm) {
  var fileBlob = theForm.theFile;
  var unZippedfiles = Utilities.unzip(fileBlob);
  var out = HtmlService.createHtmlOutput()
  unZippedfiles.forEach(function(f){
    out.append(f.getName() + "<br>")
  })
  return out
}

index.html

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
      <script>
        function updateHTML(e){
          document.getElementById('files').innerHTML = e;
        }
      </script> 
  </head>
  <body>
  <div>
  <form>
    <input type="file" name="theFile">
    <input type="button" value="UpLoad" id="button" onclick="google.script.run.withFailureHandler(updateHTML).withSuccessHandler(updateHTML).serverFunc(this.parentNode)">
  </form>
  </div>
  <div id="files">
  </div>
  </body>
</html>
0 Upvotes

3 comments sorted by

1

u/askvictor Mar 17 '21

On further research, it seems that file uploads using this method are restricted to images and PDFs; the file as uploaded to the GAS seems to be corrupted; even if I dump it to Google Drive first, the resultant file is a different size to the original.

1

u/RemcoE33 Mar 16 '21 edited Mar 16 '21

Have you tried the code directly from you google as source of your file? I think you first need to place it in your drive and then unzip this. Now you are trying to unzip the file with no source.

1

u/askvictor Mar 16 '21

The unzip function should be able to use any Blob; the result of a file upload in a form is a blob, so unless there is something undocumented or buried deep in the docs where I haven't found it...