0

I was wondering if it is possible in JS to open a directory, read an image file and display it to Html? I believe JS restricts from being able to open any file in a directory directly, but what I want is:

I have a XML file which will contain the path to a image file in the web server root folder so my hierarchy is like this

webserver root folder--->|
                        html
                        js
                        css
                        images
                        xml

, and I will use XmlHttpRequest and feed the directory tag and file name tag to my JS file which has to display the image to my frame in the Html page.

[My image is also in the same webserver root folder but in a different folder from html]

Any pointers on how to go about it? I guess we can store the image file also in XML as a base64 encoded data, but that would make the data exchange huge, also don't know if this is a ideal method (is it? please suggest)

Please give me some tips for this.

Thanks

Balaji R

devgp
  • 1,321
  • 3
  • 17
  • 36

3 Answers3

2

JavaScript does not have access to filesystem on server, since it runs on the client side.

But with JavaScript or Ajax you can call some php code on server which will read the image from the file system and then it will pass this image back to the JavaScript.

I have described here how to do this.

Community
  • 1
  • 1
Scherbius.com
  • 3,396
  • 4
  • 24
  • 44
1

If I am following you correctly, example.com/js/somefile.js is trying to access something like example.com/images/image.jpg?

If so then i would either use the absolute URL of the image:

"http://www.example.com/images/image.jpg" or the relative path "../images/image.jpg"

When referencing the images in your code you could actually use a plain text file, one image path per line. Then in your onreadystatechange function:

pictures = var.responseText.split("\n");

now pictures is an array of picture paths.

daalbert
  • 1,465
  • 9
  • 7
  • First, thanks for responding. Yea you are right i could do that, but i guess i would the other approach of Server Side Scripting. basically i would not want to type the URL for every image i have on the server. I want the script to search a directory and display all the images from that directory – devgp May 17 '11 at 22:59
  • You could have the file it reads actually a php script. In the script include a header('Content-type: text/xml') so that way it will be completely dynamic. – daalbert May 17 '11 at 23:10
0

JavaScript only has access to the information & priviledges that the browser has access to, so unless the image is in a directory that would normally be accessible on the web site, you're not going to have much luck using just JavaScript.

Is there any way that you can make the path in the filesystem available to the web document root folder? Maybe by using an Alias or Symlink?

stevecomrie
  • 2,423
  • 20
  • 28
  • Well my bad, i think i should have been more specific by mentioning what i was referring to by "filesystem". Yes filesystem is basically my web document's root folder. In my webserver i have some hierarchy like below root ---> html,images,css,js,xml. So yes in my html document if i give /> it works and i can see the image. – devgp May 17 '11 at 22:27