如何使用javascript获取文件夹中的图像列表? [英] How can I get a list of images in a folder using javascript?
问题描述
我有一个像这样的文件夹树:
I have a folder tree like so:
- ProjectFolder
- img
- fjdkslajsd.jpg
- > fjowefwask.jpg
- fweoifqadf.jpg
- img
- ...
- ProjectFolder
- img
- fjdkslajsd.jpg
- fjowefwask.jpg
- fweoifqadf.jpg
- ...
- img
- script.js li>
- jquery.js
- script.js
- jquery.js
- styles.css
- styles.css
/ em>文件几乎只是链接到脚本和样式表,并包含一个div来保存我的内容。我想要做的是在 img 文件夹中找到 script.js 图像的所有名称,并将它们放在列表中,以便我以后可以通过列表并逐个显示图像。最终结果本质上看起来像一个powerpoint,连续循环通过 img 文件夹中的图像,并显示它们。
The index.html file pretty much just links to the scripts and stylesheet, and contains a div to hold my content. What I want to do is have the script.js find all the names of the images in the img folder and put them in a list so that I can later cycle through the list and display the images one by one. The end result would essentially look like a powerpoint that continuously cycles through the images in the img folder and displays them.
我希望清除任何你的问题,所以这里是我的...我如何捕获包含 img 文件夹中的图像的名称的字符串列表?我已经知道如何循环列表和显示图像,但我不知道如何解决GETTING的列表。
I hope that clears up any of your questions, so here's mine... How would I capture a list of strings that contain the names of the images in the img folder? I already know how to cycle through the list and display the images, but I have no idea how I would tackle GETTING the list.
推荐答案
p>除非你有某种类型的接口服务器端,它给你一个可解析的结构,代表项目文件夹/文件结构,你将无法在javascript中使用或处理这些信息。
Unless you have some kind of interface server-side which gives you a parseable structure representing the project folder / file structure, you won't be able to use or handle this information in javascript.
如果您的服务器上有PHP可用,您可以使用纯输出生成一个javascript数组:
If you have PHP available on your server, you can generate a javascript array using plain output:
<script type="text/javascript">
var imageList=[<?php
$dir='/projectdir/img/';
$files = scandir($dir);
foreach((array)$files as $file){
if($file=='.'||$file=='..') continue;
$fileList[]=$file;
}
echo "'".implode("','", $fileList)."'";
?>];
</script>
浏览器解析后, imageList
包含给定文件夹中的所有文件的数组。然后,您可以使用此数组进行进一步处理/处理。
After the browser parses this, imageList
will contain an array of all the files in the given folder. You can then use this array for further processing / handling.
这篇关于如何使用javascript获取文件夹中的图像列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!