【问题标题】:InboxSDK - Unable to attachFiles through composeView objectInboxSDK - 无法通过 composeView 对象附加文件
【发布时间】:2018-12-13 18:04:51
【问题描述】:

我正在尝试使用 Inboxsdk 通过 composeView 对象附加附件。我使用以下代码从远程 url 获取 blob 对象。

// FUNCTION TO OPEN A NEW COMPOSE BOX ONCE THE ATTACHMENT BLOB IS OBTAINED FROM REMOTE URL.
function openComposeBox(sdk, blob){
  handler = sdk.Compose.registerComposeViewHandler(function(composeView){
     if(blob != null){
       composeView.attachFiles([blob]);
       composeView.setSubject("Testing");
    }
 });
}

// FETCHING ATTACHMENT FILE FROM REMOTE URL
var file_btn_url = "https://api.hummingbill.com/system/invoices/invoice_files/000/033/393/original/abracadabra.txt";
var file_name = file_btn_url.split("/");
file_name = file_name[file_name.length-1];
file_type = /[^.]+$/.exec(file_name);

var blob = null;
var xhr = new XMLHttpRequest();
xhr.open("GET", file_btn_url);
xhr.responseType = "blob";                                       
xhr.onload = function()
{
       blob = xhr.response;
    // blob.lastModifiedDate = new Date(); // Since it's not necessary to have it assigned, hence commented.
        blob.name = file_name;                                          
        console.log(blob);
      openComposeBox(sdk, blob);

}
xhr.send();

它显示 附件失败 错误。

虽然我根据documentation 的要求具有正确的 blob 对象格式。

根据文档,我已经为 blob 设置了文件名,并将其以数组的形式传递给 attachFiles 函数。你能调查一下,让我知道我错过了什么吗?

【问题讨论】:

  • 如果您在附加文件时遇到错误(使用正确的实现和配置),请确保您已安装 Flash。访问get.adobe.com/flashplayer 以检查更新并下载最新版本。还要尝试清除缓存和 cookie。您可以查看here。也可以查看这个相关的forum。
  • 欣赏建议。通过将 blob 转换为文件,然后附加来尝试它。它奏效了。
  • 嗨 anurag,您可以发布解决方案作为答案吗?谢谢!
  • @brasofilo 发布了解决方案作为答案。干杯。

标签: javascript google-inbox inboxsdk


【解决方案1】:

发布解决方案。代码与问题中的代码保持一致,略有不同,我们将 blob 转换为文件,以使其工作。

//... Same as the code in the question


// Fetching attachment file from remote url
var file_btn_url = "https://api.hummingbill.com/system/invoices/invoice_files/000/033/393/original/abracadabra.txt";
var file_name = file_btn_url.split("/");
file_name = file_name[file_name.length-1];
file_type = /[^.]+$/.exec(file_name);

var blob = null;
var xhr = new XMLHttpRequest();
xhr.open("GET", file_btn_url);
xhr.responseType = "blob";                                       
xhr.onload = function()
{
     // Solution: Convert the obtained blob to a file
     // Pass the file to openComposeBox
     blob = new Blob([xhr.response], { type: xhr.responseType });
     var file = new File([blob], file_name);
     blob.name = file_name;                     
     openComposeBox(sdk, file);
}
xhr.send();

希望这会有所帮助。干杯!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-09
    • 2020-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-06
    相关资源
    最近更新 更多