0

I implemented the tutorial from Google Apps Script documentation. However, the uploaded file is corrupted. I suspect it might be encoded, but I didn't figure out how. Any ideas?

Here it is for reference:

Code.js:

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

function processForm(formObject) {
  var formBlob = formObject.myFile;
  var driveFile = DriveApp.createFile(formBlob);
  return driveFile.getUrl();
}

index.html

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
    <script>
      // Prevent forms from submitting.
      function preventFormSubmit() {
        var forms = document.querySelectorAll('form');
        for (var i = 0; i < forms.length; i++) {
          forms[i].addEventListener('submit', function(event) {
            event.preventDefault();
          });
        }
      }
      window.addEventListener('load', preventFormSubmit);

      function handleFormSubmit(formObject) {
        google.script.run.withSuccessHandler(updateUrl).processForm(formObject);
      }
      function updateUrl(url) {
        var div = document.getElementById('output');
        div.innerHTML = '<a href="' + url + '">Got it!</a>';
      }
    </script>
  </head>
  <body>
    <form id="myForm" onsubmit="handleFormSubmit(this)">
      <input name="myFile" type="file" />
      <input type="submit" value="Submit" />
    </form>
    <div id="output"></div>
 </body>
</html>
  • 1
    If you are using V8 runtime and you are trying to upload a binary data, I thought that this thread might be useful for your situation. https://stackoverflow.com/q/60742695/7108653 – Tanaike Dec 31 '20 at 01:14
  • 1
    or this one: https://stackoverflow.com/questions/57813728/how-to-get-files-from-an-html-file-input-and-send-it-as-an-attachment-to-an-em/57814406#57814406 – Cooper Dec 31 '20 at 02:18
  • @Tanaike Yes, I am using V8 and your comments point me in the right direction. Thank you. – Thomas Daniel Jan 01 '21 at 18:12

0 Answers0