Neil C. Obremski
  • About
  • Blog
  • Music
    • Moo
    • Necros
    • colbie
    • TravelEra
  • CPP
    • Multi Dimension Arrays and Pointer Pointers
    • Structures
  • Tech
    • ASP
    • Apache2 SSL Windows
    • Celery
    • Chromebook
    • Django
    • FFMPEG
    • IIS
    • JS >
      • Save Binary File to Disk in JavaScript
      • Simple JavaScript/AJAX Optimizations
    • Kindle Self-Publishing
    • Linux Shell
    • Mac OS X
    • QB
    • Pyodbc
    • SQL
    • Subversion for VSS Users
    • VSS
    • WinHttp
    • WSH
  • Games
    • Daggerfall
    • Minecraft
    • Thief >
      • Install Thief on 2000/XP
      • Run Thief without CD
      • Fix Thief Freeze
+1 (206) 795-1327

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 ...
  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
Submit
Proudly powered by Weebly