通过 Web 应用程序通过电子邮件获取损坏或空白文件 - Google 脚本

Corrupted or Blank file is getting by email through Web app - Google script

问题

低于 google 脚本 运行 很好,但是通过电子邮件发送的上传文件在通过电子邮件发送时已损坏或空白。附加文件名、内容类型与上传的相同...但正在获取文件打不开。。文本文件没问题。。谁能帮忙解决一下。

Code.gs

function doGet() {
  return HtmlService.createHtmlOutputFromFile('index')
    .setSandboxMode(HtmlService.SandboxMode.IFRAME);
}

function processForm(formObject) {
  var myFile = formObject.myFile;

  var FileBytes = myFile.getBytes();
  var FileType = myFile.getContentType();
  var FileName = myFile.getName();
  var FileToSend = {
    fileName: FileName,
    content: FileBytes,
    mimeType: FileType
  };
  // Logger.log(FileType);

  var FileBytes2 = [100, 97, 121, 32, 108, 97, 32, 110, 111, 105, 32, 100, 117, 110, 103, 32, 98, 101, 110, 32, 116, 114, 111, 110, 103];
  var FileToSend2 = {
    fileName: 'test222.txt',
    content: FileBytes2,
    mimeType: 'text/plain'
  };
  var FileToSend3 = {
    fileName: 'test333.txt',
    content: 'noi dung ben trong',
    mimeType: 'text/plain'
  };

  GmailApp.sendEmail('email@domain', '6 Attachment example', '6 Please see the attached file.', {
    attachments: [FileToSend, FileToSend2, FileToSend3],
    name: '6 Automatic Emailer Script'
  });

  return FileName;
}

index.html


<html>

<head>
  <base target="_top">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>
  <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(filename) {
      var div = document.getElementById('output');
      div.innerHTML = filename;
    }
  </script>
</head>

<body>



  <form action="#" id="myForm" onsubmit="handleFormSubmit(this)" method="post" enctype="multipart/form-data">
    <div class="file-field input-field">
      <div class="btn">
        <span>File</span>
        <input name="myFile" type="file" multiple>
      </div>
      <div class="file-path-wrapper">
        <input class="file-path validate" type="text" placeholder="Upload one or more files">
       </div>
    </div>
          <input type="submit" value="Submit" />
  </form>
<div id="output"></div>


</body>

</html>

为了解决您的问题,在 handleFormSubmit 函数中,我采用了一个数组缓冲区并将其转换为包含文件数据的字符串并将其传递给您的 processForm 函数,以便要在前端而不是后端处理的逻辑,google.script.run 对于可以作为参数传递的值有点挑剔。因此,您的 handleFormSubmit 函数现在将如下所示:

 const handleFormSubmit = async (formObject) => {
      // Get all the file data
      let 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 = (new Uint8Array(fileBuffer)).toString();
      // Pass the file meta data and the content 
      google.script.run.withSuccessHandler(updateUrl).processForm(file.name, file.type, data);
}

至于后端函数processForm,需要将data字符串重新转换为二进制数据数组,所以我使用了JSON.parse("[" + data + "]")。现在,您的 processForm 将如下所示:

function processForm(name, type, data) {
  var fileToSend = {
    fileName: name,
    // Convert the string to a Binary data array
    content: 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";
}