【问题标题】:Corrupted or Blank file is getting by email through Web app - Google script通过 Web 应用程序通过电子邮件获取损坏或空白文件 - Google 脚本
【发布时间】:2020-06-14 08:17:08
【问题描述】:

问题

以下 google 脚本运行正常,但通过电子邮件发送的上传文件已损坏或在通过电子邮件获取时为空白。附加文件名、内容类型与上传的相同...但无法打开获取文件..文本文件很好。 .. 任何人都可以在这方面提供帮助。

代码.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>

【问题讨论】:

    标签: javascript google-apps-script file-upload large-files multiple-file-upload


    【解决方案1】:

    为了解决您的问题,在 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";
    }
    

    【讨论】:

    • 非常感谢@LeCat,这对我帮助很大,让我头疼,你是个美丽的天才!如果可以的话,我会投票给你。
    • 我很高兴我的回答对你有帮助 @AndresDuarte :D
    猜你喜欢
    • 2022-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-28
    • 1970-01-01
    • 2013-08-15
    • 1970-01-01
    • 2020-12-15
    相关资源
    最近更新 更多