Skip to content Skip to sidebar Skip to footer

Corrupted Or Blank File Is Getting By Email Through Web App - Google Script

Question Below google script running fine but the file uploaded send via email is corrupted or blank while getting through email.. Attached filename, content type are same as uplo

Solution 1:

To solve your issue, in the handleFormSubmit function, I took an array buffer and transformed it into a string containing the file data and pass it to your processForm function, in order for that logic to be handled in the frontend instead of the backend, google.script.run is a little picky with the values you can pass as arguments. Therefore, your handleFormSubmit function will look like this now:

const handleFormSubmit = async (formObject) => {
      // Get all the file datalet file = formObject.myFile.files[0];
      // Get binary content, we have to wait because it returns a promise let fileBuffer = await file.arrayBuffer();
      // Get the file content as binary and then pass it to string const data = (newUint8Array(fileBuffer)).toString();
      // Pass the file meta data and the content 
      google.script.run.withSuccessHandler(updateUrl).processForm(file.name, file.type, data);
}

As for the backend function processForm , you will need to transform the data string into a binary data array again, that's why I use JSON.parse("[" + data + "]"). Now, your processForm will look like this:

functionprocessForm(name, type, data) {
  var fileToSend = {
    fileName: name,
    // Convert the string to a Binary data arraycontent: JSON.parse("[" + data + "]"), 
    mimeType: type
  };
  GmailApp.sendEmail('email@domain', '6 Attachment example', '6 Please see the attached file.', {
    attachments: [fileToSend],
    name: '6 Automatic Emailer Script'
  });
  return"this file " + name + " has just been sent to your email";
}

Post a Comment for "Corrupted Or Blank File Is Getting By Email Through Web App - Google Script"