Google Apps 脚本 - 缺少 e.postData 和 BLOB 参数

问题描述 投票:0回答:1

在下面找不到有关我的问题的任何信息 - 所以发帖。

我正在将 BLOB 上传到 google-apps-script web 应用程序 - 代码和日志如下。

    网络应用 doPost(e)
  1. 上缺少
    e.postData
  2. e.parameter.**NewsFile**
    不见了
  3. e.contentLength
    表示已接收到数据 - 使用 1Mb BLOB 进行检查
  4. 客户端表单数据指示正在附加新闻文件
  5. 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
javascript google-apps-script web-applications
1个回答
0
投票

在您的客户端脚本中,您想要请求

multipart/form-data
。不幸的是,在现阶段,Web Apps 无法正确接收
multipart/form-data
的值。在这种情况下,您的值需要作为字符串数据发送。当你的展示脚本修改后,下面的修改如何?

修改后的脚本1:

在本次修改中,假设数据是字符串数据。

Javascript:

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) });  
}

Google Apps 脚本:

在上述 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");
}

修改后的脚本2:

在此修改中,假设数据是二进制数据。

Javascript:

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) });
  }
}

Google Apps 脚本:

在上述 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");
}

运行此脚本时,数据将作为文件在根文件夹中创建。

注:

参考资料:

© www.soinside.com 2019 - 2024. All rights reserved.