Javascript 使用Javascript获取文件夹中的文件名列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31274329/
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 list of filenames in folder with Javascript
提问by bevanb
My website is serving a lot of pictures from /assets/photos/
folder. How can I get a list of the files in that folder with Javascript?
我的网站从/assets/photos/
文件夹中提供大量图片。如何使用 Javascript 获取该文件夹中的文件列表?
回答by Christoffer Karlsson
The current code will give a list of all files in a folder, assuming it's on the server side you want to list all files:
当前代码将给出文件夹中所有文件的列表,假设它在服务器端您要列出所有文件:
var fs = require('fs');
var files = fs.readdirSync('/assets/photos/');
回答by Simpal Kumar
No, Javascript doesn't have access to the filesystem. Server side Javascriptis a whole different story but I guess you don't mean that.
不,Javascript 无权访问文件系统。服务器端 Javascript是一个完全不同的故事,但我想你不是那个意思。
回答by IfThenElse
I write a file dir.php
我写了一个文件dir.php
var files = <?php $out = array();
foreach (glob('file/*.html') as $filename) {
$p = pathinfo($filename);
$out[] = $p['filename'];
}
echo json_encode($out); ?>;
In your script add:
在您的脚本中添加:
<script src='dir.php'></script>
and use the files[] array
并使用 files[] 数组
回答by Naltroc
For client side files, you cannot get a list of files in a user's local directory.
对于客户端文件,您无法获得用户本地目录中的文件列表。
If the user has provided uploaded files, you can access them via their input
element.
如果用户提供了上传的文件,您可以通过他们的input
元素访问它们。
<input type="file" name="client-file" id="get-files" multiple />
<script>
var inp = document.getElementById("get-files");
// Access and handle the files
for (i = 0; i < inp.files.length; i++) {
let file = inp.files[i];
// do things with file
}
</script>
回答by Ahmad Zafar
For getting the list of filenames in a specified folder, you can use
要获取指定文件夹中的文件名列表,您可以使用
**fs.readdir(directory_path, callback_function)**
This will return a list which you can parse by simple list indexing like file[0],file[1]etc. Hope this helps.
这将返回一个列表,您可以通过简单的列表索引(如文件 [0]、文件 [1]等)来解析该列表。希望这会有所帮助。
回答by pietervanderstar
I use the following (stripped-down code) in Firefox 69.0 (on Ubuntu) to read a directory and show the image as part of a digital photo frame. The page is made in HTML, CSS, and JavaScript, and it is located on the same machine where I run the browser. The images are located on the same machine as well, so there is no viewing from "outside".
我在 Firefox 69.0(在 Ubuntu 上)中使用以下(精简代码)读取目录并将图像显示为数码相框的一部分。该页面是用 HTML、CSS 和 JavaScript 制作的,它位于我运行浏览器的同一台机器上。图像也位于同一台机器上,因此无法从“外部”查看。
var directory = <path>;
var xmlHttp = new XMLHttpRequest();
xmlHttp.open('GET', directory, false); // false for synchronous request
xmlHttp.send(null);
var ret = xmlHttp.responseText;
var fileList = ret.split('\n');
for (i = 0; i < fileList.length; i++) {
var fileinfo = fileList[i].split(' ');
if (fileinfo[0] == '201:') {
document.write(fileinfo[1] + "<br>");
document.write('<img src=\"' + directory + fileinfo[1] + '\"/>');
}
}
This requires the policy security.fileuri.strict_origin_policy
to be disabled. This means it might not be a solution you want to use. In my case I deemed it ok.
这需要security.fileuri.strict_origin_policy
禁用该策略。这意味着它可能不是您想要使用的解决方案。就我而言,我认为没问题。
回答by Tanmay_Gupta
I made a different route for every file in a particular directory. Therefore, going to that path meant opening that file.
我为特定目录中的每个文件制作了不同的路由。因此,转到该路径意味着打开该文件。
function getroutes(list){
list.forEach(function(element) {
app.get("/"+ element, function(req, res) {
res.sendFile(__dirname + "/public/extracted/" + element);
});
});
I called this function passing the list of filename in the directory __dirname/public/extracted
and it created a different route for each filename which I was able to render on server side.
我调用了这个函数,传递了目录中的文件名列表,__dirname/public/extracted
它为我能够在服务器端呈现的每个文件名创建了不同的路由。