jQuery 获取目录中所有文件夹的列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15537424/
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
get a list of all folders in directory
提问by Robert E. McIntosh
So I have a file system that looks like this:
所以我有一个看起来像这样的文件系统:
Music
- 001
-- song.mp3
- 002
-- song.mp3
- 003
- 004
- 005
-- song.mp3
musicplayer.html
musicplayer.js
I was curious if it is possible to get a list of all of the folders names?
我很好奇是否可以获得所有文件夹名称的列表?
This javascript, html5, or even jQuery, I can not install anything.
这个javascript,html5,甚至jQuery,我什么都装不了。
回答by internals-in
he has a point in his question (HTML5)
他的问题有一个观点(HTML5)
I was curious if it is possible to get a list about all of the folders names? This javascript, or even jQuery, I can not install anything
我很好奇是否有可能获得所有文件夹名称的列表?这个javascript,甚至是jQuery,我什么都安装不了
Simply No , but not the Last Answer !
不,但不是最后的答案!
JavaScript (JS) is an interpreted computer programming language.It was originally implemented as part of web browsers so that client-side scripts could interact with the user, control the browser, communicate asynchronously, and alter the document content that was displayed.
JavaScript (JS) 是一种解释型计算机编程语言。它最初作为 Web 浏览器的一部分实现,以便客户端脚本可以与用户交互、控制浏览器、异步通信以及更改显示的文档内容。
There are 3 major types of JavaScript namely:
JavaScript 有 3 种主要类型,即:
Client-Side JavaScript (CSJS) -- an extended version of JavaScript that enables the enhancement and manipulation of web pages and client browsers
Server-Side JavaScript (SSJS) -- an extended version of JavaScript that enables back-end access to databases, file systems, and servers
Core JavaScript -- the base JavaScript language
Client-Side JavaScript (CSJS) and Server-Side JavaScript (SSJS) are dependent on the core JavaScript and cannot work without it.
客户端 JavaScript (CSJS) 和服务器端 JavaScript (SSJS) 依赖于核心 JavaScript,没有它就无法工作。
JavaScript and the DOM provide the potential for malicious authors to deliver scripts to run on a client computer via the web. Browser authors contain this risk using two restrictions. First, scripts run in a sandboxin which they can only perform web-related actions, not general-purpose programming tasks like creating files. Second, scripts are constrained by the same origin policy: scripts from one web site do not have access to information such as usernames, passwords, or cookies sent to another site. Most JavaScript-related security bugs are breaches of either the same origin policy or the sandbox.
JavaScript 和 DOM 为恶意作者提供了通过 Web 提供在客户端计算机上运行的脚本的可能性。浏览器作者使用两个限制来控制这种风险。首先,脚本在沙箱中运行,在沙箱中它们只能执行与 Web 相关的操作,而不能执行诸如创建文件之类的通用编程任务。其次,脚本受到同源策略的限制:来自一个网站的脚本无法访问发送到另一个网站的用户名、密码或 cookie 等信息。大多数与 JavaScript 相关的安全漏洞都是对同源策略或沙箱的破坏。
There are subsets of general JavaScript — ADsafe, Secure ECMA Script (SES) — that provide greater level of security, especially on code created by third parties (such as advertisements).
有一些通用 JavaScript 的子集——ADsafe、Secure ECMA Script (SES)——提供更高级别的安全性,尤其是在第三方创建的代码(如广告)上。
I was curious if it is possible to get a list about all of the folders names? with HTML5
我很好奇是否有可能获得所有文件夹名称的列表?使用HTML5
HTML5 provides FileSystem API which may solve your thirst , at least for know :)
Read the tutorial here : http://www.html5rocks.com/en/tutorials/file/filesystem/
HTML5 提供了 FileSystem API,它可以解决您的口渴,至少是了解 :)
在这里阅读教程:http: //www.html5rocks.com/en/tutorials/file/filesystem/
another solution is to use the ugly browser Api, that i never ever recommends
Best Solutionis to use a server side language like php
另一个解决方案是使用丑陋的浏览器 Api,我从不推荐
最佳解决方案是使用像 php 这样的服务器端语言
回答by Robert E. McIntosh
Are you trying to get the client's file system? If so, web browsers usually prohibit this type of behavior for security reasons.
您是否正在尝试获取客户端的文件系统?如果是这样,出于安全原因,Web 浏览器通常会禁止此类行为。
If you're trying to list things on your web server, this can't be done without AJAXsince JavaScript runs on the client's machine. You would need to have some server code to list the directory for you, but this can pose a potential security risk.
如果您尝试在 Web 服务器上列出内容,则没有AJAX就无法做到这一点,因为 JavaScript 在客户端的机器上运行。您需要一些服务器代码来为您列出目录,但这可能会带来潜在的安全风险。
I think your best bet is to run server side code to list the directory, it's not the safest thing to do that on client side code.
我认为最好的办法是运行服务器端代码来列出目录,在客户端代码上这样做并不是最安全的做法。
回答by mahesh
If you are asking on client side Javascript, Yes, we can access in Mozilla Firefox using File API, i don't know other browsers.
如果您在客户端 Javascript 上询问,是的,我们可以使用 File API 在 Mozilla Firefox 中访问,我不知道其他浏览器。
https://developer.mozilla.org/en-US/docs/Code_snippets/File_I_O
https://developer.mozilla.org/en-US/docs/Code_snippets/File_I_O
回答by Todd
You could definitely accomplish this, as @Tariq said, with some serverside code, like php. you could submit directory paths in relation to the root via ajax (check out the jquery $.post() @ http://api.jquery.com/jQuery.post/).
正如@Tariq 所说,您绝对可以使用一些服务器端代码(例如 php)来实现这一点。您可以通过 ajax 提交与根目录相关的目录路径(查看 jquery $.post() @ http://api.jquery.com/jQuery.post/)。
<?php
if (isset($_POST['submit'])) {
//capture post varibles, sanitizing of course.
$dir = "";
$dir = $_POST['input'];
$dh = opendir($dir);
while (false !== ($filename = readdir($dh))) {
$files[] = $filename;
}
//now, do stuf with files
sort($files);
print_r($files);
}
?>
Here's the output:
这是输出:
Array
(
[0] => .
[1] => ..
[2] => bar.php
[3] => foo.txt
[4] => somedir
)
Hope this helps.
希望这可以帮助。