且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用javascript获取文件夹中的文件列表

更新时间:2022-11-27 08:10:18

由于安全问题,我担心你无法使用javascript访问本地文件或目录。

I'm afraid you cannot access local files or directories using javascript due to security issues.

编辑:我没有想过文件api所以想了一下这可能不是真的,但如果没有一些用户输入来给予许可,这仍然无法完成。

I hadn't thought about the file api so thought for a moment this might not be true, but without some user input to give permission, this still cannot be done.

这个问题得到了PhilNicholas的好评:

This question has a good response from PhilNicholas:


恐怕我可能是坏人的承担者您的设计的新闻:您请求的操作
明确违反了File API规范中指定的
的安全模型。 FileReader()的客户端实现必须
确保用户首先选择了FileReader对象
正在读取的所有文件。 (W3C File API,13。安全
注意事项: http:// www .w3.org / TR / FileAPI / #security- discussion )。

这将是一个巨大的安全风险,浏览器脚本只需
在没有任何用户
交互的情况下,任意打开并读取路径中的任何文件。没有任何浏览器制造商允许对整个文件系统进行无限制的访问

It would be a huge security risk of browser scripts could just arbitrarily open and read any file from a path without any user interaction. No browser manufacturer would allow unfettered access to the entire file system like that.

考虑一下,如果它是所有都在本地运行,您可以使用ajax查询可能返回您请求的目录的服务器端脚本。

Thinking about it however, if it is all being run locally, you could use ajax to query a server side script that could return the directory you request.