简体   繁体   English

谷歌脚本从外部blob创建文件

[英]google script create file from external blob

I'm getting a PDF file from an external web resource using ajax.我正在使用 ajax 从外部网络资源获取 PDF 文件。 I want to store this PDF in the google drive using google script API from within google docs.我想使用谷歌文档中的谷歌脚本 API 将此 PDF 存储在谷歌驱动器中。

Sample of that ajax call:该ajax调用的示例:

 $.ajax({
      url: "<fancyurl>",
      contentType: 'application/octet-stream',
      responsetype: 'blob',
      type: 'GET',
      success: function(response) {

 // or converted response.. etc.. 

google.script.run.withSuccessHandler(yay).withUserObject(this).createFile(response);

});

The response from the webresource is an octet-stream:来自网络资源的响应是一个八位字节流:

在此处输入图像描述

I've tried to import the original response, the response converted to blob, a uint8 blob and the base64string.我尝试导入原始响应、转换为 blob 的响应、uint8 blob 和 base64string。 All end up in errors or corrupt files.所有这些都以错误或损坏的文件告终。

   var blob = Utilities.newBlob(data, 'application/pdf' ,'asdf.pdf');
   // blob.setName('asdf.pdf');
   // blob.setContentTypeFromExtension();
   DriveApp.createFile(blob);

Where data is the response or converted response.其中 data 是响应或转换后的响应。

Does anybody know how to solve this or what google script expects as a valid input?有谁知道如何解决这个问题或谷歌脚本期望什么作为有效输入?

In my experience, when the binary data is retrieved using ajax, the binary data is converted to the text data.根据我的经验,当使用 ajax 检索二进制数据时,二进制数据会转换为文本数据。 By this, I'm worried that responsetype of blob and arraybuffer might not be able to be used.这样,我担心可能无法使用blobarraybufferresponsetype I thought that this might be the reason of your issue.我认为这可能是您的问题的原因。 So, in this answer, I would like to propose using "XMLHttpRequest" instead of "ajax".所以,在这个答案中,我想建议使用“XMLHttpRequest”而不是“ajax”。

The modified script is as follows.修改后的脚本如下。

Modified script:修改后的脚本:

From:从:

 $.ajax({
      url: "<fancyurl>",
      contentType: 'application/octet-stream',
      responsetype: 'blob',
      type: 'GET',
      success: function(response) {

 // or converted response.. etc.. 

google.script.run.withSuccessHandler(yay).withUserObject(this).createFile(response);

});

To:至:

const xhr = new XMLHttpRequest();
xhr.open('GET', "<fancyurl>", true);
xhr.responseType = 'arraybuffer';
xhr.onload = function(e) {
  google.script.run.withSuccessHandler(yay).withUserObject(this).createFile([...new Int8Array(this.response)]);
};
xhr.send();

In this modification, the binary data is converted to the array buffer and converted it to int8 array.在此修改中,将二进制数据转换为数组缓冲区并将其转换为 int8 数组。 By this, this data can be used as the byte array with Google Apps Script.这样,此数据可以用作带有 Google Apps 脚本的字节数组。

In this case, although I cannot see your whole script of Google Apps Script, you can use your Google Apps Script as follows.在这种情况下,虽然我看不到您的整个 Google Apps Script 脚本,但您可以按如下方式使用您的 Google Apps Script。

function createFile(data) {
  var blob = Utilities.newBlob(data, 'application/pdf', 'asdf.pdf');
  DriveApp.createFile(blob);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM