5

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

4

2 回答 2

1

Potential repeat of Local file access with Javascript.

My understanding is that you can't access the local filesystem directly through a web browser, you have to use an intermediary like the form input tag or drag and drop.

You may be able to get away with accessing the filesystem if you were to use the operating system's javascript interpreter or something like V8. There may also be experimental javascript api's in Chrome that you could look for on the Chrome flags page if thats your browser of choice. That all depends on whether or not you were doing a personal project or something for the web.

Otherwise another scripting language such as PHP, Ruby, or Python would better suit your needs.

于 2013-01-19T18:55:25.927 回答
-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>";
    }
}

?>
于 2013-01-19T17:43:57.867 回答