0

I am trying to attach an attachment through the composeView object using Inboxsdk. I obtain a blob object from a remote url using the following code.

// 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();

It shows an Attachment Failed error. Attachment Failed

Attachment failed inside compose box

Although I have the correct format of blob object as required as per the documentation.

Blob object

As per the documentation, I have set the filename for the blob, and passed it in an array to attachFiles function. Can you please look into it and let me know what am I missing?

4

1 回答 1

0

发布解决方案。代码与问题中的代码保持一致,略有不同,我们将 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();

希望这可以帮助。干杯!

于 2018-12-13T10:04:51.393 回答