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

前端 未结 3 1873
礼貌的吻别
礼貌的吻别 2020-12-01 17:43

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() a

相关标签:
3条回答
  • 2020-12-01 17:49

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

    0 讨论(0)
  • 2020-12-01 17:50

    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 讨论(0)
  • 2020-12-01 18:02

    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.

    0 讨论(0)
提交回复
热议问题