Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2504
  • Last Modified:

Reading binary data using ActiveX


I want to read some binary data using ActiveX. I got the following piece of code as a start:
<script LANGUAGE="JavaScript1.2">
function binaryRead(path) {
    var stream, binaryStream;  
    stream = new ActiveXObject("ADODB.Stream");
    stream.Type = 1;
        binaryStream = stream.Read();
    return binaryStream;
<body onload="return binaryRead('c:/myfile.bin')">

My question now is: How can I loop through the bytes of the file and save the values in a JavaScript-array?

  • 2
1 Solution

The trick however is getting the binary codes from files into and out of the String object in the first place, whether at all, or efficiently.

I have been experimenting myself, and so far Javascript alone does not seem to be enough.


There are two ActiveX objects to consider:-

(a) Scripting.FileSystemObject

Through Javascript, it seems to be impossible to use this to save strings containing a binary stream. If encoding is set to ASCII, an error will be thrown for char codes in the range 80 - A0 which you try to write to a file. If encoding is Unicode, then the resulting file is prefixed with FF FE, and it only works for even numbered streams.

You can use it to load binary into Strings, but you need again to watch for characters 80-A0, as they are converted during the load process to some other unicode characters. This is consistently done, so once you know the conversion, i.e. 80->2030 etc, you can easily reverse it.

However, oddly, in VBScript, with encoding set to ASCII you can actually write binary using Scripting.FileSystemObject. I think this is because VBScript has other data types which are more acceptable to the ActiveX object.

So you can adopt a mixture of VBScript and Javascript to get a kind of binary i/o.

(a) ADODB.Stream

This gives full binary file system access. I think however it is disabled by default on later versions of windows. In any event, it has one limitation again in Javascript if you want to write binary data from a String to the stream: the WriteText method again fails with characters in the range 80-A0.

(c) XML dataType

A third area to explore is setting an XML node data type to bin.hex or bin.base64. You could convert a String to base64 or hex, and set the nodeTypedValue with that encoded string. This is more useful for HTTPRequests, but conversion to base64 and hex is very slow. When you subsequently bet the nodeTypedValue it will return a Variant byte buffer containing the binary. Useless in Javascript, but might be useable with ADODB.Stream or VBScript.


jespersahnerAuthor Commented:
->rama_krishna580: Thanks for your quick and thorough answer.

I don't know if I get you wrong, but the problem is not WRITING binary data. This is taking care of on the server using Java, so my problem is on the client-side READING the binary data. However maybe the problem remains the same or..?

You mentions special characters and as I understand you this is mainly a problem when dealing with plain text. In my case I only consider binary information.

But in general why is this so difficult in JavaScript? In Java it is straightforward using e.g. InputStream-objects:
InputStream is = new FileInputStream("data.bin");
int b = is.read();
while (b != -1) {
    b = is.read();

Can the same opereration be carried out in JavaScript I wonder?

jespersahnerAuthor Commented:
->rama_krishna580: As you point out reading binary data is disabled for safety reasons. However I have written a piece of Java-code, that reads a binary file using URLConnection and DataInputStream and returns an array of doubles. This array is then returned to JavaScript.

In other words it is straightforward to read binary data using Java, and the same data can be shared with JavaScript. But what about safety-problems? I think that this is taken care of because data is encapsulated in a URL.

A "philosophical" question:
With all the fuzz about Ajax and similar technologies turning the browser more and more into a real application when will lightweight-JavaScript be replaced by heavyweight-Java now that the "war" between Microsoft and Sun is over concerning which Java JRE/SDK to use?


Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now