javascript - How to create binary blob from atob - currently getting different bytes -


i've got binary excel file created on server i'm returning c# webmethod using convert.tobase64string(filedata) called javascript/jquery $ajax call. i've confirmed base64 string data gets client, when attempt convert binary blob , save it, bytes saved disk aren't same on server. (i'm getting lots of 0xc3 etc. bytes, suspiciously utf8 double byte injections)

$.ajax({     type: "post",     contenttype: "application/json;",     datatype: "json",     processdata: false,     data: "{ inputdata: \"" + datastring + "\" }",     url: "api.aspx/getexcel",     success: ... 

success handler code includes:

var excelblob = new blob([atob(msg.d)], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;' }); ... var = document.createelement('a'); ... a.href = window.url.createobjecturl(excelblob); a.setattribute('download', 'excel.xlsx'); 

when completes download has bad bytes values. binary comparison source shows it's close has c3 , similar values inserted or munged place.

is there i'm doing wrong or missing base64 string correctly converted client binary blob?

the new blob constructor encodes strings encounters utf-8 (http://dev.w3.org/2006/webapi/fileapi/#constructorblob). since dealing binary data get's converted utf-8 multi-byte representations.

instead need convert data array of bytes before passing blob constructor.

the following code works me in chrome:

var binary = atob(base64) var array = new uint8array(binary.length) for( var = 0; < binary.length; i++ ) { array[i] = binary.charcodeat(i) } new blob([array]) 

that said don't know if atob defined across browsers (i'm guessing there's reason mozilla provides longer example code https://developer.mozilla.org/en-us/docs/web/api/windowbase64/base64_encoding_and_decoding#solution_.232_.e2.80.93_rewriting_atob%28%29_and_btoa%28%29_using_typedarrays_and_utf-8).


Comments

Popular posts from this blog

c++ - OpenMP unpredictable overhead -

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

javascript - Wordpress slider, not displayed 100% width -