html5 - Download Binary Data as a File Via Javascript -
i'm making ajax call api returns binary data. i'm wondering if possible take binary data , display client in new window? i'm doing right now. problem is, document opens up, blank.
$.ajax({ type: "post", url: apiurl, data: xmlrequest, complete: function(xhr, status) { var bb = new window.webkitblobbuilder(); // append binary data blob bb.append(xhr.responsetext); var bloburl = window.webkiturl.createobjecturl(bb.getblob('application/pdf')); window.open(bloburl); } });
any ideas?
okay, figured out. had specify responsetype 'array buffer':
function downloadpdf() { var xhr = new xmlhttprequest(); xhr.open('post', api_url, true); xhr.responsetype = 'arraybuffer'; xhr.onload = function(e) { if (this.status == 200) { var bb = new window.webkitblobbuilder(); bb.append(this.response); // note: not xhr.responsetext var blob = bb.getblob('application/pdf'); var bloburl = window.webkiturl.createobjecturl(blob); window.open(bloburl); } }; xhr.send(createrequest()); }
Comments
Post a Comment