title: Google Script fails to upload file
body: <p>I'm attempting to add a menu option in a Google Sheet that uploads a file to drive, then inserts a link to that file in the Sheet. I'm having problems with the upload function, called from HTML.</p>
  <p><strong>Code.gs</strong></p>
  <pre class="lang-js prettyprint-override"><code>function onOpen(e){
    Logger.log('Opened spreadsheet');
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var menuEntries = [];
    menuEntries.push({name: &quot;File&quot;, functionName: &quot;openHTMLUploadDialogue&quot;});
    ss.addMenu(&quot;Attach!&quot;, menuEntries);
  }
  
  function openHTMLUploadDialogue(e) {
    var html = HtmlService.createHtmlOutputFromFile('UploadForm');
    SpreadsheetApp.getUi().showModalDialog(html, 'Upload file');
    // This is printed
    Logger.log('dialog opened');
  }
  
  function upload(obj) {
  
    // This is never printed
    Logger.log('Obj received: ', obj.fname);
  
    //Retrieve the input data of the Form object.
    var newFileName = obj.fname;
    var rowNum = obj.position;
    var blob = obj.file;
  
    var upFile = DriveApp.getFolderById(getParentFolderID()).createFile(blob).setName(newFileName);
    var fileUrl = upFile.getUrl();
  
    var urlCell = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1').getRange(rowNum,5);
    urlCell.setValue('=HYPERLINK(&quot;' + fileUrl + '&quot;,&quot;View file&quot;)');
  
  }
  
  function getParentFolderID(){
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var file = DriveApp.getFileById(ss.getId());
    var folderinDrive = file.getParents().next();
    Logger.log(folderinDrive.getName());
  
    return folderinDrive.getId();
  }
  </code></pre>
  <p><strong>UploadForm.HTML</strong></p>
  <pre class="lang-html prettyprint-override"><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
    &lt;head&gt;
      &lt;base target=&quot;_center&quot;&gt;
      &lt;link rel=&quot;stylesheet&quot; href=&quot;https://ssl.gstatic.com/docs/script/css/add-ons1.css&quot;&gt;
          &lt;script src=&quot;https://code.jquery.com/jquery-3.4.1.js&quot; integrity=&quot;sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU=&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;
    &lt;/head&gt;
    &lt;body&gt;
    &lt;form id=&quot;myForm&quot;&gt;
        Please upload image below.&lt;br /&gt;&lt;br /&gt;
      &lt;input type=&quot;hidden&quot; name=&quot;fname&quot; id=&quot;fname&quot; value=&quot;&lt;?= fName ?&gt;&quot;/&gt;
      &lt;input type=&quot;hidden&quot; name=&quot;position&quot; id=&quot;position&quot; value=&quot;&lt;?= position ?&gt;&quot;/&gt;
      &lt;input type=&quot;file&quot; name=&quot;file&quot; id=&quot;file&quot; accept=&quot;image/jpeg,.pdf&quot; /&gt;
      &lt;input type=&quot;button&quot; value=&quot;Submit&quot; class=&quot;action&quot; onclick=&quot;formData(this.parentNode)&quot; /&gt;
      &lt;input type=&quot;button&quot; value=&quot;Close&quot; onclick=&quot;google.script.host.close()&quot; /&gt;
    &lt;/form&gt;
    &lt;script&gt;
    //Disable the default submit action  using “func1”
     window.onload=func1;
     function func1() {
        document.getElementById('myForm').addEventListener('submit', function(event) {
          console.log('Submit pressed!');
              event.preventDefault();
            });  
     }
  
     function formData(obj){
       // This line is printed
        console.log('Received obj: ', obj);
        // This line always fails
        google.script.run
        .withSuccessHandler(closeIt)
        .withFailureHandler(onFailure)
        .upload(obj);
     } 
  
     function onFailure(e){
        console.log(e.name);
        console.log(e.message);
     } 
  
    function closeIt(e){
        console.log(e);
        google.script.host.close();
    };   
  
      &lt;/script&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  <p>The output of the Sheet inspector console is:</p>
  <pre class="lang-none prettyprint-override"><code>Fo {message: &quot;There was an error during the transport or process…this request. Error code = 10, Path = /wardeninit&quot;, name: &quot;TransportError&quot;, stack: &quot;TransportError: There was an error during the tran…4020103-warden_bin_i18n_warden__en_gb.js:195:263)&quot;}
  userCodeAppPanel:13 Received obj:  &lt;form id=​&quot;myForm&quot;&gt;​…​&lt;/form&gt;​
  4094020103-warden_bin_i18n_warden__en_gb.js:89 Net state changed from IDLE to BUSY
  4094020103-warden_bin_i18n_warden__en_gb.js:89 Net state changed from BUSY to IDLE
  4094020103-warden_bin_i18n_warden__en_gb.js:89 Net state changed from IDLE to BUSY
  4094020103-warden_bin_i18n_warden__en_gb.js:89 Net state changed from BUSY to IDLE
  userCodeAppPanel:22 ScriptError
  userCodeAppPanel:23 We're sorry, a server error occurred. Please wait a bit and try again.
  </code></pre>
  <p>And the output of the Google Script console only shows <code>Opened spreadsheet</code> and <code>dialog opened</code>.</p>
  <p>The <code>Sheets</code> and <code>Drive</code> services are enabled, it seems like it never manages to return to the Code.gs upload function.</p>