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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20265195/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 18:06:59  来源:igfitidea点击:

how get list of files in a folder using javascript

javascripthtmlvisual-c++-2010-express

提问by Chintan Soni

I am working on project for desktop application. I am using Qtcontrols with visual c++. I am loading an html file in the QWebView as,

我正在开发桌面应用程序项目。我正在使用带有 Visual C++ 的Qt控件。我在 QWebView 中加载一个 html 文件,

m_pWebView->load(QUrl("../../../demo/index_Splash_Screen.html"));

Now, what i want is, say, I have some .zipfiles in my location "c:\demo", I want list (or array of file names) of the files present in that directory.

现在,我想要的是,比如说,.zip我的位置“c:\demo”中有一些文件,我想要该目录中存在的文件的列表(或文件名数组)。

How can i do this through javascript ?

我怎样才能通过 javascript 做到这一点?

PS:I went through this link, but it didnt match my requirement. I have not worked with of html, javascript and jquery. Please help me.

PS:我浏览了这个链接,但它不符合我的要求。我没有使用过 html、javascript 和 jquery。请帮我。

回答by Alfie

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

由于安全问题,恐怕您无法使用 javascript 访问本地文件或目录。

Edit: 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.

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

This questionhas a good response from PhilNicholas:

这个问题得到了 PhilNicholas 的很好的回答:

I'm afraid I may be the bearer of bad news for your design: The action you are requesting expressly violates the security model as specified in the File API spec. The client implementation of FileReader() must make sure that "all files that are being read by FileReader objects have first been selected by the user." (W3C File API , 13. Security Considerations: 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.

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

浏览器脚本可以随意打开和读取路径中的任何文件而无需任何用户交互,这将是一个巨大的安全风险。没有浏览器制造商会允许像这样不受限制地访问整个文件系统。

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.

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

回答by SoulSeek

If it is a Windows application then you could access the local filesystem by using ActiveX objects. You might have a look at this link Reading a txt file from Javascript

如果它是 Windows 应用程序,那么您可以使用 ActiveX 对象访问本地文件系统。您可能会查看此链接从 Javascript 读取 txt 文件

Note that activeX usage is possible only when using IE as browser/engine; I used to need it a while ago for developing an HTML application (.hta files).

请注意,仅当使用 IE 作为浏览器/引擎时才可以使用 activeX;不久前我曾经需要它来开发 HTML 应用程序(.hta 文件)。