有没有办法只使用Javascript从文件夹中返回所有图像文件名列表? [英] Is there a way to return a list of all the image file names from a folder using only Javascript?

查看:95
本文介绍了有没有办法只使用Javascript从文件夹中返回所有图像文件名列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示位于图片库中服务器上的文件夹中的所有图片。

I want to display all images from folder located in server in image gallery.

有没有办法从文件夹中返回所有图像文件名的列表仅使用JavaScript或JQuery?

Is there a way to return a list of all the image file names from a folder using only JavaScript or JQuery?

此外,我想仅使用JavaScript计算类似文件夹中的图像文件数。

Also I want to count the number of image files in similar folder using only JavaScript.

有没有办法只使用JavaScript计算文件夹中的图像文件数?

Is there any way to count the number of image files from a folder using only JavaScript?

推荐答案

不,你可以不要单独使用Javascript来做到这一点。客户端Javascript无法以我认为您要求的方式读取目录的内容。

No, you can't do this using Javascript alone. Client-side Javascript cannot read the contents of a directory the way I think you're asking about.

但是,如果您能够将索引页添加到(或配置您的Web服务器以显示images目录的索引页面,并且您从同一服务器提供Javascript,然后您可以进行AJAX调用以获取索引,然后解析它。

However, if you're able to add an index page to (or configure your web server to show an index page for) the images directory and you're serving the Javascript from the same server then you could make an AJAX call to fetch the index and then parse it.

ie

1)在Apache上为yoursite.com上的相关目录启用索引:

1) Enable indexes in Apache for the relevant directory on yoursite.com:

http://www.cyberciti.biz/faq/enabling -apache-file-directory-indexing /

2)然后使用jQuery获取/解析它。你必须弄清楚如何最好地抓取页面,并且几乎肯定有一种更有效的方法来获取整个列表,但是一个例子:

2) Then fetch / parse it with jQuery. You'll have to work out how best to scrape the page and there's almost certainly a more efficient way of fetching the entire list, but an example:

$.ajax({
  url: "http://yoursite.com/images/",
  success: function(data){
     $(data).find("td > a").each(function(){
        // will loop through 
        alert("Found a file: " + $(this).attr("href"));
     });
  }
});

这篇关于有没有办法只使用Javascript从文件夹中返回所有图像文件名列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆