Javascript读取文件夹中的文件 [英] Javascript read files in folder

查看:119
本文介绍了Javascript读取文件夹中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下问题,我想用JavaScript解决。我有一个背景图像在css文件中指定的div,我想让我的javascript定期更改该图像(让我们说每5秒)。

I have the following problem which I'm trying to solve with javascript. I have a div with a background image specified in a css file, and I want my javascript to change that image periodically (let`s say every 5 secs).

我知道如何做到这一点,问题是,我有一个图像文件夹选择从后面的图像。我需要能够读取文件名(从图像文件夹)到一个数组,改变div的背景图像,延迟5秒,然后再次更改。

I know how to do that, the problem is that I have a folder of images to choose from for the back image. I need to be able to read the filenames (from the image folder) into an array, change the background image of the div, delay for 5 seconds and change again.

推荐答案

你仍然需要php或asp来查询文件夹中的文件。 Javascript将无法远程检查文件系统。

You're still going to need php or asp to query the folder for files. Javascript will not be able to "remotely" inspect the file system.

您可以在jQuery中执行以下操作:

You can do something like the following in jQuery:

$.ajax({
  url: 'getFolderAsArrayOfNames.php',
  dataType: 'json',
  success: function(data) {
    for(var i=0;i<data.length;i++) {
      // do what you need to do
    }
  });
});

在您的getFolderAsArrayOfNames.php中,像这样:

And in your getFolderAsArrayOfNames.php, something like this:

echo "function "
     .$_GET['callback']
     ."() {return "
     .json_encode(scandir('somepath/*.jpg'))
     ."}";

这篇关于Javascript读取文件夹中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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