Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Reading binary data using ActiveX

Posted on 2006-11-17
Medium Priority
Last Modified: 2008-02-01

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?

Question by:jespersahner
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 23

Accepted Solution

rama_krishna580 earned 1000 total points
ID: 17970487

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.



Author Comment

ID: 17970615
->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?


Author Comment

ID: 18152735
->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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question