最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How can I iterate through files in Javascript - Stack Overflow

programmeradmin4浏览0评论

Is it possible to iterate through a collection of files in Javascript? I am writing a jQuery plugin that takes either an array of images or, I was hoping, a directory containing a list of images. E.g. either:

['image1.jpg','image2.jpg','image3.jpg']

or '/'

I would then like to be able to iterate through the above domain and take the file name from each of the files in the folder.

I have read about the FileSystemObject but this will only be available in IE, is there an equivalent that can be used in all browsers?

Is it possible to iterate through a collection of files in Javascript? I am writing a jQuery plugin that takes either an array of images or, I was hoping, a directory containing a list of images. E.g. either:

['image1.jpg','image2.jpg','image3.jpg']

or 'http://somedomain./images/'

I would then like to be able to iterate through the above domain and take the file name from each of the files in the folder.

I have read about the FileSystemObject but this will only be available in IE, is there an equivalent that can be used in all browsers?

Share Improve this question asked Mar 1, 2010 at 20:20 FerminFermin 36.2k22 gold badges87 silver badges131 bronze badges 2
  • What are you trying to acplish? What do you want to do with each file? – Josh Stodola Commented Mar 1, 2010 at 20:27
  • 1 @stereofrog - The files are located on server. @Josh Stodola - The images will be loaded into the page, it's to save passing, say 20 image names in an array, I was wondering if the user could just pass the location of hte images. – Fermin Commented Mar 1, 2010 at 20:32
Add a ment  | 

4 Answers 4

Reset to default 1

You should use server side language to provide array of images in JSON or similar. There is no way Javascript can access local files. That would be serious security issue.

If the directory is on the same domain, and your webserver is setup to show directory indexes, then you could use javascript to request the url and load it into the dom for data extraction. This is brittle, and doesn't sound like it would make for a good plugin, much less good for anything but a one off task. But, you could do it.

The other suggestions of having serverside script output json or xml would be an improvement.

You can't.

You have to get the list from your server through JSON or XML. Because of this, you can't do this in any way with javascript on servers you have no control over. Also, access to client side files is actively avoided for security reasons.

You would need to create a web service to fetch the list of files. You can't use ajax to directly fetch the directory listing because ajax calls are restricted to the domain.

发布评论

评论列表(0)

  1. 暂无评论