简体   繁体   English

相对于root的Javascript列表文件

[英]Javascript list files relative to root

This is for an offline application. 这适用于离线应用程序。

In html , we can access files relative to an html page. html ,我们可以访问相对于html页面的文件。 Eg: 例如:

  • <script src="scripts/jquery-3.3.1.min.js"></script>
  • <link rel='stylesheet' href='styles/style.css'>
  • <img src='./images/file1.jpg'/>

If I have many images in /images directory, is there a way to list all images in the directory using Javascript , or would I need to hard code the image names? 如果我在/images目录中有很多图像,有没有办法使用Javascript列出目录中的所有图像,或者我需要硬编码图像名称?

I'm asking specifically for using Javascript and without Node, running the files straight from Chrome. 我要求专门使用Javascript和没有Node,直接从Chrome运行文件。

Usually the client doesn't have access to the directory tree of the server. 通常,客户端无权访问服务器的目录树。 That means any client-side javascript, will not be able to list the directory tree. 这意味着任何客户端javascript都无法列出目录树。

You can configure apache to list the file-tree if there is no index file present, but usually that is frowned upon. 如果没有索引文件,您可以配置apache列出文件树,但通常不赞成。

If you (against my recommendation) want to do this, you can configure apache to list the files and then fetch the file list with JavaScript. 如果您(违反我的建议)想要这样做,您可以配置apache列出文件,然后使用JavaScript获取文件列表。

A much better way to do this is to use server-side code (Node.js Server, PHP, etc.) which has access to the filesystem and can provide a list of files. 更好的方法是使用服务器端代码(Node.js Server,PHP等),它可以访问文件系统并可以提供文件列表。

Local only application 仅本地应用程序

Javascript usually doesn't have access to local resources and will throw a CORS error when trying to fetch normal resources. Javascript通常无法访问本地资源,并且在尝试获取普通资源时会抛出CORS错误。 (The CORS error is thrown wether you're in a local directory already or not.) This makes this pretty much impossible to do without a webserver. (无论你是否在本地目录中,都会抛出CORS错误。)这使得没有网络服务器这几乎是不可能的。

try run chrome.exe --allow-file-access-from-files 尝试运行chrome.exe --allow-file-access-from-files

by running the command line above, it will enable http call using : 通过运行上面的命令行,它将使用以下命令启用http调用:

$.get("file:///url", function(data) {})

note that the data is returning string. 请注意,数据正在返回字符串。 if you append it into your html it will show the directory 如果你将它附加到你的HTML中,它将显示该目录

Hand -coded not unless you use a web server (you don't need to install one, there are many tools to serve a single directory such as PHP or Python built-in servers). 手动编码,除非您使用Web服务器(您不需要安装Web服务器,有许多工具可以为单个目录提供服务,例如PHPPython内置服务器)。

But hard -coded yes: You can't access local filesystem from javascript, but your browser can if you type the ( file:// ) url in the navigation bar or follow a link pointing to it. 编码是:您无法从javascript访问本地文件系统,但如果您在导航栏中键入( file:// )url或按照指向它的链接,您的浏览器就可以。

In case of links browsers refuse them in remote pages (from a web server) but not from files that are already local (otherwise you couldn't see a downloaded version of a web page). 如果是链接,浏览器会拒绝远程页面(来自Web服务器),而不是来自本地文件(否则您无法看到网页的下载版本)。

I've never tested with absolute paths but (I think) it should work if you have permissions to that directory. 我从来没有测试过绝对路径但是(我认为)如果你对该目录有权限它应该可以工作。

If not, there exists browser extensions, such as this one for Firefox that allows to open that links (if the user accepts it). 如果没有,则存在浏览器扩展,例如用于Firefox的浏览器扩展,允许打开该链接(如果用户接受它)。

The problem is that, as I said, you need to put the links manually in the page and if there are so many it could take some time unless you are used to a real editors such as Vim or, at least, you are able to generate it using any programming language you like. 问题是,正如我所说,你需要手动将链接放在页面中,如果有这么多,可能需要一些时间,除非你习惯了像Vim这样的真正的编辑,或者至少,你能够使用您喜欢的任何编程语言生成它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM