开发者

How to get the modified time of a file being uploaded in JavaScript?

开发者 https://www.devze.com 2023-04-10 08:08 出处:网络
Is there ever a way possible to get the actual creation / modification time of the file being uploaded, using JavaScript?

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 / ti开发者_StackOverflow中文版me 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.


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.


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


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);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号