Javascript - access data of a file local to the document

Started by
4 comments, last by markr 10 years, 6 months ago

Hi!

I wonder how to access a file resource that is local to the document in javascript, at binary or text form.

Consider

<body>

<img src="/localfile.bmp" id="pic">

<body>

How would I show to the user 4th byte value of the file resource in the pic element?

Advertisement

any thoughts?

Image elements don't work on that level. You can't ask the Image object for its binary data. You can in principle extract the pixel data from an image, which might be an idea for loading maps or other assets.

If you want to do file IO for arbitrary (non-image) files, use XMLHttpRequest.

Thanks, but request would perform server communication whenever I need to read something, or I would have to keep the data in memory. If I was to be able to read data in local resources, I would actualy read temp files that browser downloaded from my side. Would browser allow me to use file streaming in the document local resources?

I'm not a HTML 5 wizard, but I believe you may be able to use the new <canvas> tag, by drawing the image to the canvas and then extracting the pixels from the canvas.

You need to "notionally" do network access to get these data for images. But the images might come from the browser's cache if they're already available. You can encourage or enforce caching through a combination of web server headers and a cache-manifest file. Or you can just preload your images when your game starts so they're already available.

Once you have an Image object in memory, and you're holding a reference to it, it's not going to go away and you can use it without a problem. But when a player comes back to play the game again (in the same browser session or a different one), the assets need to come either from the server or the browser cache.

This topic is closed to new replies.

Advertisement