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?

brasofilo
  • 25,496
  • 15
  • 91
  • 179
anurag
  • 2,176
  • 2
  • 22
  • 41
  • 1
    If you encounter an error while attaching a file (with correct implementation and configuration), make sure that you have Flash installed. Visit http://get.adobe.com/flashplayer/ to check for updates and download the latest version. Also try to clear the cache and cookies. You can check it [here](https://productforums.google.com/forum/#!topic/gmail/p-Z1sfo6bAM). You can also check this related [forum](https://groups.google.com/forum/#!msg/inboxsdk/-UXahXvmYv4/JGD5FwssEQAJ). – abielita Oct 06 '16 at 10:54
  • 1
    Appreciate the Suggestion. Tried it by converting the blob to file, and then attaching. It worked. – anurag Oct 06 '16 at 11:09
  • Hi anurag, can you post the solution as an answer? Thanks! – brasofilo Dec 08 '18 at 17:32
  • @brasofilo posted the solution as an answer. Cheers. – anurag Dec 13 '18 at 10:05

1 Answers1

0

Posting the solution. The code remains same as in the question, with a slight variation, wherein we convert the blob to a file, in order to make it work.

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

Hope this helps. Cheers!

anurag
  • 2,176
  • 2
  • 22
  • 41