Solved

Reading binary data using ActiveX

Posted on 2006-11-17
4
2,240 Views
Last Modified: 2008-02-01
Hi!

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

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

Regards,
Jesper
0
Comment
Question by:jespersahner
  • 2
4 Comments
 
LVL 23

Accepted Solution

by:
rama_krishna580 earned 250 total points
ID: 17970487
Hi,

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.

INTERNET EXPLORER

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.

http://support.microsoft.com/kb/193998
http://support.microsoft.com/kb/168335
http://psacake.com/web/gj.asp

R.K
0
 

Author Comment

by:jespersahner
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();
}
is.close();

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

Regards,
Jesper
0
 

Author Comment

by:jespersahner
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?


Regards,
Jesper
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now