Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can display image on webpage just cant get its name

I can display an image:

...
<img src="img.png">
...

So I know its on the server, but I cant simply print its name.

For example, if its in the root directory, I don’t know how to do something like:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

var directory = "./"
for file in get_files(directory):
    console.log(file) # should print the files in the directory

Thanks for any help.

>Solution :

JavaScript running in the browser has no way of determining what URLs are recognised by an HTTP server without either:

  • Being told about them (e.g. by <img src>)
  • Making an HTTP request and seeing what the response is

The is no standard HTTP feature for getting listing of URLs that are available under a given path.

Sometimes a web server will be configured so that if you ask for /foo/ then it will return an HTML document with links to every resource directly under /foo/.

If that is the case you could use the fetch api to make a request for that HTML document and then parse it, query for the links, then loop over the result and read the href attributes.

In that isn’t the case, as in the example you gave in the comments where https://tok1n.codeberg.page/fs_test/uploads/pdf/ is a 404 Not Found error page that that approach won’t work unless you are capable of changing the server to provide a directory listing.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading