Save Binary File to Disk in JavaScript
...I am consuming an XML web service using javascript..
One of the elements is a base64 encoded byte array... so it is a binary array of a document from my webserver...
I REALLY need to know how I can from the client write this file to disk on my client using javascript... I don’t mind if I have to use ActiveXObjects.
An answer ...
Warning: This will only work on Windows machines running Internet Explorer (or another ActiveX-capable web browser). It will also be suppressed unless the website it is running from is set as Trusted or the user specifically allows the actions.
Example ...
- Decode the base64-encoded data into a string using my base64.js or similar.
- Create a ADODB.Stream object, configure it for Text Mode (2) and the ISO-8859-1 Charset.
- Finally just call WriteText() followed by SaveToFile() and Close().
Warning: This will only work on Windows machines running Internet Explorer (or another ActiveX-capable web browser). It will also be suppressed unless the website it is running from is set as Trusted or the user specifically allows the actions.
Example ...
var data = DecB64(base64_encoded_string); var stream = new ActiveXObject("ADODB.Stream"); stream.Type = 2; // text stream.Charset = "ISO-8859-1"; stream.Open(); stream.WriteText(data); stream.SaveToFile("C:\\Whatever.dat", 2); stream.Close();
References ...