Flash中的文件夹中的Foreach文件?

时间:2010-06-02 20:07:10

标签: flash image file-io actionscript-3 foreach

我现在有一个图像幻灯片程序,它接收一个硬编码的图像文件夹。我想改变它,以便它可以接收一个文件夹,无论数字如何都会显示所有这些文件夹。有没有办法在flash中执行此操作?我正在考虑像perl或其他脚本语言中的foreach循环。可以在文本文件中存储数量的图像,但我也不知道如何在flash中读取它。我正在使用动作3.我将非常感谢任何帮助。

谢谢-Mike

2 个答案:

答案 0 :(得分:1)

试试这个:

var folder : File = new File('path');
folder.addEventListener(FileListEvent.DIRECTORY_LISTING, dirListHandler);
folder.getDirectoryListingAsync();

--

private function dirListHandler(event : FileListEvent) : void
{
   for each(var file : File in event.files)
   {
      trace(file.url);
   }
}

您需要为此编译到AIR应用程序。

HTH

答案 1 :(得分:1)

@Zarate是正确的,您需要使用服务器端脚本语言。

如果您选择PHP,请查看readdir,其中“返回目录中下一个文件的文件名”。 [PHP Manual]

这是我创建的用于检索目录中所有文件的文件名的PHP类:

class DirectoryContentsHandler
{
    private $directory;
    private $directoryHandle;
    private $dirContents = array();

    public function __construct($directory)
    {
      $this->directory = $directory;
      $this->openDirectory();
      $this->placeDirFilenamesInArray();
    }

    public function openDirectory()
    {
      $this->directoryHandle = opendir($this->directory);
      if(!$this->directoryHandle)
      {
        throw new Exception('opendir() failed in class DirectoryContents at openDirectory().');
      }
    }

    public function placeDirFilenamesInArray()
    {
      while(false !== ($file = readdir($this->directoryHandle)))
      {
        if(($file != ".") && ($file != ".."))
        {
            $this->dirContents[] = $file;
        }
      }
    }

    public function getDirFilesAsArray()
    {
      return $this->dirContents;
    }

    public function __destruct()
    {
      closedir($this->directoryHandle);
    }
}

以下是如何使用上面列出的类:

$directoryName = 'some_directory/';
//Instantiate the object and pass the directory's name as an argument
$dirContentsHandler = new DirectoryContentsHandler($directoryName);
//Get the array from the object
$filesArray = $dirContentsHandler->getDirFilesAsArray();
//Display the contents of the array for this example:
var_dump($filesArray);

除此之外,您可以回显数组的内容并将它们作为一串变量发送到SWF,或者(如果有很多图像,这是更好的选择)使用PHP创建一个XML文件包含文件名,然后将该文件发送到SWF。从那里,使用Actionscript来解析XML,加载图像文件,并在客户端显示它们。