16

In my Ext Js solution I am calling a service which is returning this JSON format

{"success":true,"filename":"spreadsheet.xlsx","file":[80,75,3,4,20,0,...(many more)]}

How can I make a file download dialog with the filename and the content of the byte array (file) ?

UPDATE

So I found this bit to start the downlaod

var a = window.document.createElement('a');
                    a.href = window.URL.createObjectURL(new Blob(data.file, { type: 'application/octet-stream' }));
                    a.download = data.filename;

                    // Append anchor to body.
                    document.body.appendChild(a)
                    a.click();

                    // Remove anchor from body
                    document.body.removeChild(a)

So far good

But the file I get is corrupted so I suspect I need to Encode/Decode the file variable?

1

3 Answers 3

36

I had to convert the file into a Uint8Array before passing it to the Blob

var arr = data.file;
var byteArray = new Uint8Array(arr);
var a = window.document.createElement('a');

a.href = window.URL.createObjectURL(new Blob([byteArray], { type: 'application/octet-stream' }));
a.download = data.filename;

// Append anchor to body.
document.body.appendChild(a)
a.click();


// Remove anchor from body
document.body.removeChild(a)

Reading this answer helped a lot https://stackoverflow.com/a/16245768/1016439

2
2

Building on Jepzen's response, I was able to use this technique to download a document from AWS S3 from within the browser. +1 Jepzen

s3.getObject(params, function(err, data) {
      if (err === null) {
         var arr = data.Body;
         var byteArray = new Uint8Array(arr);
         var a = window.document.createElement('a');

         a.href = window.URL.createObjectURL(new Blob([byteArray], { type: 'application/octet-stream' }));
         a.download = fName; //fName was the file name portion of the key what was passed in as part of the key value within params. 

         // Append anchor to body.
         document.body.appendChild(a)
         a.click();

         // Remove anchor from body
         document.body.removeChild(a)
      } else {
        result = 'failure'
         console.log("Failed to retrieve an object: " + err);
      }
});
   

0

The below solution worked for me:

var date = new Date();
var filename = "Orders_ProductDetails_" + "_" +
    date.getFullYear() +
    "." +
    date.getDate() + "." +
    (date.getMonth() + 1);

const url = window.URL.createObjectURL(response); // here response = api res converted to blob like res.blob()
const a = document.createElement('a');
a.style.display = 'none';
a.href = url;
// the filename you want
a.download = filename+'.xlsx';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
window.URL.revokeObjectURL(url);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.