Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 通过Web app-Google脚本通过电子邮件获取损坏或空白文件_Javascript_Google Apps Script_File Upload_Large Files_Multiple File Upload - Fatal编程技术网

Javascript 通过Web app-Google脚本通过电子邮件获取损坏或空白文件

Javascript 通过Web app-Google脚本通过电子邮件获取损坏或空白文件,javascript,google-apps-script,file-upload,large-files,multiple-file-upload,Javascript,Google Apps Script,File Upload,Large Files,Multiple File Upload,问题 下面的谷歌脚本运行良好,但通过电子邮件上传的文件在通过电子邮件时已损坏或为空。。附加的文件名、内容类型与上载的相同。。。但无法打开获取文件。。文本文件很好。。。在这方面谁能帮忙呢 代码.gs function doGet() { return HtmlService.createHtmlOutputFromFile('index') .setSandboxMode(HtmlService.SandboxMode.IFRAME); } function processForm(

问题

下面的谷歌脚本运行良好,但通过电子邮件上传的文件在通过电子邮件时已损坏或为空。。附加的文件名、内容类型与上载的相同。。。但无法打开获取文件。。文本文件很好。。。在这方面谁能帮忙呢

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


//阻止表单提交。
函数preventFormSubmit(){
var forms=document.queryselectoral('form');
对于(var i=0;i
为了解决您的问题,在
handleFormSubmit
函数中,我获取了一个数组缓冲区,并将其转换为包含文件数据的字符串,然后将其传递给您的
processForm
函数,以便在前端而不是后端处理该逻辑,对于可以作为参数传递的值有点挑剔。因此,您的
handleFormSubmit
函数现在将如下所示:

const handleFormSubmit=async(formObject)=>{
//获取所有文件数据
让file=formObject.myFile.files[0];
//要获取二进制内容,我们必须等待,因为它会返回一个承诺
让fileBuffer=wait file.arrayBuffer();
//以二进制形式获取文件内容,然后将其传递给字符串
const data=(新的Uint8Array(fileBuffer)).toString();
//传递文件元数据和内容
google.script.run.withSuccessHandler(updateUrl.processForm)(file.name、file.type、data);
}
至于后端函数
processForm
,您需要将
数据
字符串再次转换为二进制数据数组,这就是为什么我使用
JSON.parse(“[”+数据+“])
)。现在,您的
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,这帮了我很大的忙,还帮了我一个头疼的忙,你真是个漂亮的天才!如果可以的话,我会投你一票。我真的很高兴我的回答对你有帮助@Andresduart:d
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";
}