Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 1

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 dont mind if I have to use ActiveXObjects.
An answer ...
1. Decode the base64-encoded data into a string using my base64.js or similar.
2. Create a ADODB.Stream object, configure it for Text Mode (2) and the ISO-8859-1
Charset.
3. 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 ...
MSDN: ADO SaveToStream

You might also like