jquery - Download File from Bytes in JavaScript -
i want download file coming in form of bytes ajax response.
i tried way of bolb
:
var blob=new blob([resultbyte], {type: "application/pdf"}); var link=document.createelement('a'); link.href=window.url.createobjecturl(blob); link.download="myfilename.pdf"; link.click();
it in fact downloading pdf file file corrupted.
how can accomplish this?
i asked question long time age, might wrong in details.
blob
turned out needs array buffers. that's why base64 bytes need converted array buffers first.
here function that:
function base64toarraybuffer(base64) { var binarystring = window.atob(base64); var binarylen = binarystring.length; var bytes = new uint8array(binarylen); (var = 0; < binarylen; i++) { var ascii = binarystring.charcodeat(i); bytes[i] = ascii; } return bytes; }
here function save pdf file:
function savebytearray(reportname, byte) { var blob = new blob([byte]); var link = document.createelement('a'); link.href = window.url.createobjecturl(blob); var filename = reportname + ".pdf"; link.download = filename; link.click(); };
here how use these 2 functions together:
var samplearr = base64toarraybuffer(data); savebytearray("sample report", samplearr);
Comments
Post a Comment