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

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <pre> <div> <blockquote> <object> <embed> <img> <param>
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.

More information about formatting options

This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Copy the characters (respecting upper/lower case) from the image.