Jump to content
  • Advertisement
Sign in to follow this  
JohnnyCode

Javascript - access data of a file local to the document

This topic is 2100 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Advertisement

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.

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!