Link to home
Start Free TrialLog in
Avatar of jespersahner
jespersahner

asked on

Reading binary data using ActiveX

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
ASKER CERTIFIED SOLUTION
Avatar of rama_krishna580
rama_krishna580
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jespersahner
jespersahner

ASKER

->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
->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