Gunah Gaar
Gunah Gaar

Reputation: 535

HTML5 read files from path

Well, using HTML5 file handlining api we can read files with the collaboration of inpty type file. What about ready files with pat like /images/myimage.png etc??

Any kind of help is appreciated

Upvotes: 1

Views: 165

Answers (2)

Shahid Karimi
Shahid Karimi

Reputation: 4357

Yes, if it is chrome! Play with the filesytem you will be able to do that.

Upvotes: 3

user1467267
user1467267

Reputation:

The simple answer is; no. When your HTML/CSS/images/JavaScript is downloaded to the client's end you are breaking loose of the server.

Simplistic Flowchart

  • User requests URL in Browser (for example; www.mydomain.com/index.html)
  • Server reads and fetches the required file (www.mydomain.com/index.html)
  • index.html and it's linked resources will be downloaded to the user's browser
  • The user's Browser will render the HTML page
    • The user's Browser will only fetch the files that came with the request (images/someimages.png and stuff like scripts/jquery.js)

Explanation

The problem you are facing here is that when HTML is being rendered locally it has no link with the server anymore, thus requesting what /images/ contains file-wise is not logically comparable as it resides on the server.

Work-around

What you can do, but this will neglect the reason of the question, is to make a server-side script in JSP/PHP/ASP/etc. This script will then traverse through the directory you want. In PHP you can do this by using opendir() (http://php.net/opendir).

With a XHR/AJAX call you could request the PHP page to return the directory listing. Easiest way to do this is by using jQuery's $.post() function in combination with JSON.

Caution!

You need to keep in mind that if you use the work-around you will store a link to be visible for everyone to see what's in your online directory you request (for example http://www.mydomain.com/my_image_dirlist.php would then return a stringified list of everything (or less based on certain rules in the server-side script) inside http://www.mydomain.com/images/.

Notes

  • http://www.html5rocks.com/en/tutorials/file/filesystem/ (seems to work only in Chrome, but would still not be exactly what you want)
  • If you don't need all files from a folder, but only those files that have been downloaded to your browser's cache in the URL request; you could try to search online for accessing browser cache (downloaded files) of the currently loaded page. Or make something like a DOM-walker and CSS reader (regex?) to see where all file-relations are.

Upvotes: 1

Related Questions