javascript - 通过 Web 应用程序通过电子邮件获取损坏或空白文件 - 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;
}
索引.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";
}
推荐阅读
- raspberry-pi - Contiki os and rapsberry PI 3
- sql-server - INSERT 和 UPDATE 如何根据 IF 条件工作?
- javascript - Array type using Array
is forbidden - arrays - PostgreSQL 使用位置数组访问数组中的多个元素
- sql - 如何从 PL/SQL 中的给定表中找出所有约束?
- excel - Excel sum based on matrix condition and multiple criteria
- .net - Why can't generic extension method be called with explicit generic arguments?
- c - 使用 C 编程在 Eclipse 中扩展二维数组的维度
- ios - How to enable split keyboard for iPad in the iOS Simulator
- python - 如何断开与烧瓶的连接