在javascript / html5中查看新文件的本地目录

时间:2013-01-19 15:48:00

标签: javascript html html5

正如标题所说,是否可以监控真实文件系统中的本地目录(而不是html5沙箱)?我想写一个自动照片上传器,寻找新照片并上传它们。

2 个答案:

答案 0 :(得分:1)

可能重复Local file access with Javascript

我的理解是您无法通过Web浏览器直接访问本地文件系统,您必须使用表单输入标记之类的中介或拖放。

如果您要使用操作系统的javascript解释器或类似V8之类的东西,您可能无法访问文件系统。 Chrome中可能还有一些实验性的javascript api,你可以在Chrome标记页面上查找,如果这是您选择的浏览器。这一切都取决于你是否在为个人项目或网络做些什么。

否则,其他脚本语言(如PHP,Ruby或Python)将更适合您的需求。

答案 1 :(得分:-1)

您可以设置Javascript Timing事件。即:使用 setInterval() 方法。

另一方面,您可以创建一个按钮来触发 onClick event 或任何其他事件,以执行以下代码。

注意:

如果设置间隔,请确保在再次发送之前收到请求。

为实现此目的,您需要检查 XML HTTP请求 readyState 是否等于4,如下所示:

xmlhttp.readyState == 4

注意:

这是用于发送请求,解析响应并将其放入Javascript数组中:

xmlhttp = new XMLHttpRequest();
xmlhttp.open("POST", "check_dirs.php", true);
xmlhttp.send();

fileArray = new Array();

xmlhttp.onreadystatechange = function()
{
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
    {
        xmlDoc = xmlhttp.responseXML;

        fileList = xmlDoc.getElementsByTagName("filesChanged");

        while (fileArray.length > 0)

        // clean the whole array.
        // we want to store the newly generated file list
        {
                    fileArray.pop();
        }

        for (i = 0; i < fileList.length; i++)
        {
            fileArray[fileArray.length] = fileList[i].childNodes[0].nodeValue;
        }
    }
} 

此外,您需要编写一些 PHP script 来检查您的自定义目录,查找比给定日期更新的文件,可以在请求中发送,并发送回复 XML响应,如下所示:

<?php

(...) // check dir. output $files contain the xml nodes for the files to send

      // mockup below


  // Get our XML. You can declare it here or even load a file.
  $xml_builder = '<?xml version="1.0" encoding="utf-8"?>';

  $xml_builder .= $files;

  // We send XML via CURL using POST with a http header of text/xml.
  $ch = curl_init('http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']);
  curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
  curl_setopt($ch, CURLOPT_HEADER, 0);
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_builder);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
  curl_setopt($ch, CURLOPT_REFERER, 'http://www.hello..co.uk');
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  $ch_result = curl_exec($ch);
  curl_close($ch);

  /*
  echo $ch_result;
  */
?>

以下是一些用于检查目录和构建XML响应的模型函数:

<?php

function analizeDir($dir)
{
    if (is_dir($dir))
    {
        $dir_resource = opendir($dir);
        while (false !== ($res = readdir($dir_resource)))
        {
            if ($res != "." && $res != ".." && $res != "old")
            {
                if (is_dir($dir . "\\" . $res)) // this is a subforder
                {
                    analizeDir($dir . "\\" . $res);

                } else { // this is a file

                    checkFile($dir . "\\" . $res);
                }
            }
        }
    }
}

function checkFile($file)
{
    $today = date("Y-m-d H:i:s");

    // if the difference in days between today
    // and the date of the file is more than 10 days,
    // print it in the response

    if (date_diff(datemtime($file), $today) > 10)
    {
         $files .= "<filesChanged>" . $file . "</filesChanged>";
    }
}

?>