Solved

Reading binary data using ActiveX

Posted on 2006-11-17
4
2,318 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
[X]
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
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Fix Form size HTML 16 31
jQuery on click requires double click 6 33
how can i break an array into a couple 5 26
Problem in javascript 17 26
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 …
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
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…

733 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