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: http://www.heypage.com/nagoon97/BinFileReader/BinFileReader.js
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 usedreadNumber(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”);
bmpFile.movePointerTo(18);
var width = bmpFile.readNumber(4);
var height = bmpFile.readNumber(4);
* Demo:
http://www.heypage.com/nagoon97/BinFileReader/BinFileReader_demo.htmlhttp://www.heypage.com/nagoon97/BinFileReader/MosaicBuilder_demo.html
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).
Delicious
Digg
StumbleUpon
Propeller
Reddit
Magnoliacom
Newsvine
Furl
Facebook
Google
Yahoo
Technorati
Icerocket
Recent comments
4 weeks 1 day ago
5 weeks 3 days ago
6 weeks 4 days ago
6 weeks 5 days ago
7 weeks 4 hours ago
7 weeks 2 days ago
7 weeks 3 days ago
9 weeks 5 days ago
10 weeks 21 hours ago
10 weeks 23 hours ago