可以在网页上显示图像,只是无法获取其名称。

huangapple go评论70阅读模式
英文:

Can display image on webpage just cant get its name

问题

我可以显示一张图片:

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

所以我知道它在服务器上,但我不能简单地打印出它的名称。

例如,如果它在根目录中,我不知道如何执行类似以下的操作:

var directory = "./"
for file in get_files(directory):
    console.log(file) # 应该打印目录中的文件

感谢任何帮助。

英文:

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:

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

Thanks for any help.

答案1

得分: 2

JavaScript在浏览器中运行时,没有办法确定HTTP服务器识别哪些URL,除非:

  • 通过其他方式告知(例如通过<img src>
  • 发送HTTP请求并查看响应是什么

没有标准的HTTP功能可以获取给定路径下可用的URL列表。

有时,Web服务器会配置成如果你请求/foo/,它将返回一个HTML文档,其中包含直接在/foo/下的每个资源的链接。

如果是这种情况,你可以使用fetch API来请求该HTML文档,然后解析它查询链接,然后循环遍历结果并读取href属性

但如果情况不是这样,就像你在评论中提到的例子,其中https://tok1n.codeberg.page/fs_test/uploads/pdf/是一个404 Not Found错误页面,这种方法将无法工作,除非你有能力更改服务器以提供目录列表。

英文:

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.

huangapple
  • 本文由 发表于 2023年6月15日 19:15:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76481903.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定