在下面找不到有关我的问题的任何信息 - 所以发帖。
我正在将 BLOB 上传到 google-apps-script web 应用程序 - 代码和日志如下。
e.postData
e.parameter.**NewsFile**
不见了e.contentLength
表示已接收到数据 - 使用 1Mb BLOB 进行检查chrome.devtools
显示发送到服务器的正确负载我使用 FormData 是因为我需要以编程方式将文件/blob 附加到发送请求。
非常感谢任何想法/见解。
// client code
function clientTest() {
const content = '<q id="a"><span id="b">hey!</span></q>';
const blob = new Blob([content], { type: "text/xml" });
const formdata = new FormData();
formdata.append("TestKey", "TestValue");
formdata.append("NewsFile", blob);
console.log(`client send info :: ${[...formdata.keys()].join(" | ")}`);
fetch("webappurl", { method: "POST", body: formdata });
}
// client log
// client send info :: TestKey | NewsFile
// server code - google apps script webapp
function doPost(e) {
console.info(`e.keys = ${[...Object.keys(e)].join(" | ")}`);
console.info(`param.keys = ${[...Object.keys(e.parameter)].join(" | ")}`);
console.info(`post.length = ${e.contentLength}`);
}
// server log
// e.keys = parameters | contextPath | parameter | contentLength | queryString | pathInfo
// param.keys = TestKey
// post.length = 684
在您的客户端脚本中,您想要请求
multipart/form-data
。不幸的是,在现阶段,Web Apps 无法正确接收 multipart/form-data
的值。在这种情况下,您的值需要作为字符串数据发送。当你的展示脚本修改后,下面的修改如何?
在本次修改中,假设数据是字符串数据。
function clientTest() {
const content = '<q id="a"><span id="b">hey!</span></q>';
const obj = { content, "TestKey": "TestValue" };
fetch("webappurl", { method: "POST", body: JSON.stringify(obj) });
}
在上述 Javascript 的情况下,可以按如下方式检索值
content
和 TestKey
。
function doPost(e) {
// DriveApp.createFile("sample.txt", JSON.stringify(e)); // When you use this line, the event object can be seen as a text file.
const obj = JSON.parse(e.postData.contents);
const { content, TestKey } = obj;
return ContentService.createTextOutput("ok");
}
在此修改中,假设数据是二进制数据。
function clientTest() {
const content = '<q id="a"><span id="b">hey!</span></q>';
const blob = new Blob([content], { type: "text/xml" });
const f = new FileReader();
f.readAsArrayBuffer(blob);
f.onload = d => {
const buf = new Int8Array(d.target.result);
const obj = { content: [...buf], mimeType: "text/xml", "TestKey": "TestValue" };
fetch("webappurl", { method: "POST", body: JSON.stringify(obj) });
}
}
在上述 Javascript 的情况下,可以按如下方式检索值
content
、mimeType
和 TestKey
。
function doPost(e) {
// DriveApp.createFile("sample.txt", JSON.stringify(e)); // When you use this line, the event object can be seen as a text file.
const obj = JSON.parse(e.postData.contents);
const { content, mimeType, TestKey } = obj;
DriveApp.createFile(Utilities.newBlob(content, mimeType, "sample"));
return ContentService.createTextOutput("ok");
}
运行此脚本时,数据将作为文件在根文件夹中创建。
在您的显示脚本中,似乎使用了Web Apps。当您修改Web Apps的Google Apps脚本时,请将部署修改为新版本。这样,修改后的脚本就会反映在Web Apps中。请注意这一点。
您可以在我的报告“在不更改 Web 应用程序 URL 的情况下重新部署 Web 应用程序(作者:我)”中查看详细信息。