File download a byte array as a file in javascript / Extjs

后端 未结 2 1663
再見小時候
再見小時候 2020-12-29 11:03

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,         


        
2条回答
  •  醉梦人生
    2020-12-29 11:44

    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

提交回复
热议问题