如何在JavaScript中获取正在上传的文件的修改时间?

发布于 2024-12-07 04:25:11 字数 226 浏览 2 评论 0 原文

有没有办法使用 JavaScript 获取正在上传的文件的实际创建/修改时间?

对于PHP,使用filectime()filemtime(),它只显示文件上传的日期/时间,而不显示文件实际创建/修改的时间在来源上。

简而言之,我想要的是在上传之前/期间/之后检查文件的 m-time(只要可能),并决定是否将文件存储在服务器上,并将相同的情况报告给客户端。

Is there ever a way possible to get the actual creation / modification time of the file being uploaded, using JavaScript?

As for PHP, using filectime() and filemtime(), it only shows the date / time the file is uploaded, and not the time the file is actually created / modified on the source.

In short, what I want is to check the m-time of a file before/during/after upload (where-ever possible) and decide whether or not to store the file on the server, and report the same back to the client.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦太阳 2024-12-14 04:25:11

如果您谈论的是用户计算机上的文件日期/时间,您可以通过 文件 API支持),它提供lastModified,这是日期/时间为自纪元以来的毫秒数(如果您想要一个Date,您可以将其传递给new Date)。 (还有已弃用的 lastModifiedDate,但是文件 API 在现代浏览器中得到普遍支持(您要使用的特定功能是 File 对象)。您可以从 File 对象获取值,并将该信息包含在单独的(例如,隐藏的)字段中。

以下是读取上次修改日期的一个粗略但完整的示例(live copy

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
<title>Show File Modified</title>
<style type='text/css'>
body {
    font-family: sans-serif;
}
</style>
<script type='text/javascript'>

    function showFileModified() {
        var input, file;

        // Testing for 'function' is more specific and correct, but doesn't work with Safari 6.x
        if (typeof window.FileReader !== 'function' &&
            typeof window.FileReader !== 'object') {
            write("The file API isn't supported on this browser yet.");
            return;
        }

        input = document.getElementById('filename');
        if (!input) {
            write("Um, couldn't find the filename element.");
        }
        else if (!input.files) {
            write("This browser doesn't seem to support the `files` property of file inputs.");
        }
        else if (!input.files[0]) {
            write("Please select a file before clicking 'Show Modified'");
        }
        else {
            file = input.files[0];
            write("The last modified date of file '" + file.name + "' is " + new Date(file.lastModified));
        }

        function write(msg) {
            var p = document.createElement('p');
            p.innerHTML = msg;
            document.body.appendChild(p);
        }
    }

</script>
</head>
<body>
<form action='#' onsubmit="return false;">
<input type='file' id='filename'>
<input type='button' id='btnShowModified' value='Show Modified' onclick='showFileModified();'>
</form>
</body>
</html>

:您无法从服务器上上传的文件获取时间的原因是请求中仅传输文件的内容,而不是客户端的文件系统元数据。

If you're talking about the file date/time on the user's machine, you can get that via the File API (support), which provides lastModified, which is the date/time as a number of milliseconds since The Epoch (if you want a Date, you can pass that into new Date). (There's also the deprecated lastModifiedDate, but that is deprecated and not supported on Safari [at least].) The File API is universally supported in modern browsers (the particular feature you'd be using is the File object). You'd get the value from the File object and include that information in a separate (for instance, hidden) field.

Here's a rough-but-complete example of reading the last modified date (live copy):

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
<title>Show File Modified</title>
<style type='text/css'>
body {
    font-family: sans-serif;
}
</style>
<script type='text/javascript'>

    function showFileModified() {
        var input, file;

        // Testing for 'function' is more specific and correct, but doesn't work with Safari 6.x
        if (typeof window.FileReader !== 'function' &&
            typeof window.FileReader !== 'object') {
            write("The file API isn't supported on this browser yet.");
            return;
        }

        input = document.getElementById('filename');
        if (!input) {
            write("Um, couldn't find the filename element.");
        }
        else if (!input.files) {
            write("This browser doesn't seem to support the `files` property of file inputs.");
        }
        else if (!input.files[0]) {
            write("Please select a file before clicking 'Show Modified'");
        }
        else {
            file = input.files[0];
            write("The last modified date of file '" + file.name + "' is " + new Date(file.lastModified));
        }

        function write(msg) {
            var p = document.createElement('p');
            p.innerHTML = msg;
            document.body.appendChild(p);
        }
    }

</script>
</head>
<body>
<form action='#' onsubmit="return false;">
<input type='file' id='filename'>
<input type='button' id='btnShowModified' value='Show Modified' onclick='showFileModified();'>
</form>
</body>
</html>

The reason you couldn't get the time from the uploaded file on the server is that only the content of the file is transmitted in the request, not the client's filesystem metadata.

颜漓半夏 2024-12-14 04:25:11

JavaScript 无法访问本地文件系统,因此如果不使用 Flash、Java 或 Active-x,则无法获取此信息。

JavaScript does not have access to the local filesystem, so you can't get to this information without using Flash, Java or Active-x.

香草可樂 2024-12-14 04:25:11

也许您可以使用 javascript 获取上次修改时间,然后在其他 javacript 中使用该时间进行排序。这次将是格林威治标准时间。

var xmlhttp = createXMLHTTPObject();
xmlhttp.open("HEAD", "http://myurl/interesting_image.jpg" ,true);
xmlhttp.onreadystatechange=function() {
  if (xmlhttp.readyState==4) {
    alert("Last modified: "+
     var lastModTimeForInterestingImage = xmlhttp.getResponseHeader("Last-Modified"))
  }
}
xmlhttp.send(null);

Perhaps you could use javascript to get the last modified time, then use that in some other javacript to sort on that. This time will be in GMT.

var xmlhttp = createXMLHTTPObject();
xmlhttp.open("HEAD", "http://myurl/interesting_image.jpg" ,true);
xmlhttp.onreadystatechange=function() {
  if (xmlhttp.readyState==4) {
    alert("Last modified: "+
     var lastModTimeForInterestingImage = xmlhttp.getResponseHeader("Last-Modified"))
  }
}
xmlhttp.send(null);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文