Convert base 64 encoded data to binary data with JavaScript
When I fire a async call to 开发者_开发问答a server, I receive the base64 data of a PDF file (binary data). I want to convert this base64 data to binary and give the user as a file from browser side. I found downloadify for downloading. But I am still stuck with the part to convert base 64 encoded data to binary data in JS
Why convert ? ;) Just serve your link as data:application/pdf;base64,
and then you base64 encoded pdf.
Will not work in old IE, of course. But should work in most modern browsers.
http://en.wikipedia.org/wiki/Data_URI_scheme
Even if you converted the base64 data to binary in javascript (and I find it extremely strange that a server would even do that - just send the binary data in the first place), you will not be able to generate a download link in javascript.
So there is no point in writing the conversion code.
Have the server send the binary data directly with the proper Content-Type:
and Content-Disposition:
headers.
You may even be able to add the Content-Encoding: base64
header and the browser will convert it on its own.
精彩评论