[W] HTML form & GAS Image file issue

68 views
Skip to first unread message

井上朋哉

unread,
Aug 21, 2024, 10:08:04 AM8/21/24
to Google Apps Script Community
I cannot successfully post image data to the `doPost` function in GAS from this HTML file:

<!DOCTYPE html>
<html>
<body>
  <form method="POST" action="deployUrl" enctype="multipart/form-data">
    <input type="file" name="image" accept="image/*">
    <input type="submit" value="Upload">
  </form>
</body>
</html>

Here is my GAS code:

function doGet(e) {
  return HtmlService.createTemplateFromFile('index').evaluate();
}

function doPost(e) {
  console.log(e);
  console.log("e.parameter: " + JSON.stringify(e.parameter));
  console.log("e.parameters: " + JSON.stringify(e.parameters));
  console.log("e.contentLength: " + e.contentLength);
  console.log("e.files: " + JSON.stringify(e.files));
 
  if (e.files && e.files.image) {
    var fileBlob = e.files.image;
    console.log("File Blob: " + fileBlob.getName());
   
    var folder = DriveApp.getFolderById('myFolerId');
    var file = folder.createFile(fileBlob);
    return ContentService.createTextOutput('File uploaded: ' + file.getUrl());
  } else {
    return ContentService.createTextOutput('No file uploaded or e.files is undefined.');
  }
}

Please let me know how to correct it.
Additionally, I want to maintain the sequence of doGet → HTML → doPost → HTML because I intend to use Gemini for image analysis afterward and transition to a page with the input content and analysis results.

olivercre...@gmail.com

unread,
Aug 22, 2024, 7:28:24 AM8/22/24
to Google Apps Script Community
Hi,

Instead of using doPost, how about trying to use google.script.run and passing in the form? Like in this example: https://developers.google.com/apps-script/guides/html/communication#forms.

Best,
Oliver

Message has been deleted

井上朋哉

unread,
Aug 22, 2024, 7:57:54 AM8/22/24
to Google Apps Script Community
I solved the problem thx.

Reply all
Reply to author
Forward
0 new messages