[Fixed] Google Apps Script Web App HTML form file-input fields not in blob compatible format

After V8 runtime was released, there was a bug that when the file is sent from HTML form to Google Apps Script side using google.script.run, the file blob was the invalid data.

From Apps Script Pulse by Martin Hawksey, it was found that the invalid blob of sending the file of HTML form to Google Apps Script side using google.script.run has finally been resolved. In this case, this script can be used with V8 runtime.

HTML & Javascript side: index.html

<form>
  <input
    type="file"
    name="file"
    onchange="google.script.run.withSuccessHandler(console.log).upload(this.parentNode)"
  />
</form>

Google Apps Script side: Code.gs

const doGet = (_) => HtmlService.createHtmlOutputFromFile("index.html");
const upload = ({ file }) => DriveApp.createFile(file).getId();

IMPORTANT:

As an important point, in the current stage, when the above HTML is accessed as Web Apps (doGet), this script works. But when the above HTML is accessed as a dialog and a sidebar, file at the Google Apps Script side is undefined. By this, it seems that in the current stage, this HTML can be used with only Web Apps. Please be careful about this. I would like to believe that this situation is resolved in the future update.

 Share!