From this page you can share Reading binary files using Ajax to a social bookmarking site or email a link to the page.
Social WebE-mail
Enter multiple addresses on separate lines or separate them with commas.
Reading binary files using Ajax
(Your Name) has forwarded a page to you from Ajaxonomy
(Your Name) thought you would like to see this page from the Ajaxonomy web site.

Reading binary files using Ajax


Over at Nagoon97’s Weblog they have an interesting post on reading a binary file in JavaScript. The post explains how the code was taken from another post and a comment that made the code work cross browser. With the trend of Ajax development going toward moving as much to the client as is prudent it is good to have a method for reading binary files should the use ever arise.

Below is an excerpt from the post.

So I put them all together in this BinFileReader function, which provides cross-browser support and helper methods to easily access the binary contents.


* BinFileReader:

getFileSize() Return the file size
getFilePointer() Return the reading position
movePointerTo(iTo) Move the reading position to iTo.
movePointer(iDirection) Move the reading position in iDirection.
For all the following read functions,

- The reading position will move to the end of where the content is last read

- When iFrom is ommited, current reading position is used
readNumber(iNumBytes, iFrom) Read and return iNumBytes-byte number starting at iFrom.
readString(iNumChars, iFrom) Read and return iNumChars characters starting at iFrom.
readUnicodeString(iNumChars, iFrom) Read and return iNumChars unicoded characters starting at iFrom.


* Example:

var bmpFile = new BinFileReader(”image.bmp”);


var width = bmpFile.readNumber(4);

var height = bmpFile.readNumber(4);


* Demo:

You can read the full post here.

This script could be used to make some interesting applications, now if we could only write binary files to the system (I know that there would be a huge security hurdle to get pass). If you use this script then I would love to hear about your experience with it (as usual you can leave it in the comments or write a blog post about it using your free Ajaxonomy account).