Is it possible to retrieve the last modified date of a file using Javascript?

前端 未结 5 1843
慢半拍i
慢半拍i 2020-12-08 16:10

I have a set of links on a web page that link to PDF forms and .doc forms. These files are not stored in a database, simply stored as they are, locally on the server. Is i

相关标签:
5条回答
  • 2020-12-08 16:44

    This seems to be useful, and works for me - giving you the 'local' date

    document.lastModified 
    

    Compared to the above selection of req.getResponseHeader() it's one less round trip/http call.

    0 讨论(0)
  • 2020-12-08 16:58

    File.lastModified

    You can use the File.lastModified property to obtain the last modified date of a file as the number of milliseconds since the Unix epoch.

    Example:

    const file = document.getElementById('input').files[0];
    const lastModifiedDate = new Date(file.lastModified);
    
    console.log(`Last Modified Date: ${lastModifiedDate}`);
    
    0 讨论(0)
  • 2020-12-08 17:03

    No, it's not. You can't access the file system through JavaScript

    0 讨论(0)
  • 2020-12-08 17:05

    If an interface is exposed through HTTP, you can. Another way of saying: expose a WebService end-point to gain access to this information.

    Of course, you can't have direct access to the filesystem for security reasons.

    0 讨论(0)
  • 2020-12-08 17:08

    If it's on the same server as your calling function you can use XMLHttpRequest-

    This example is not asynchronous, but you can make it so if you wish.

    function fetchHeader(url, wch) {
        try {
            var req=new XMLHttpRequest();
            req.open("HEAD", url, false);
            req.send(null);
            if(req.status== 200){
                return req.getResponseHeader(wch);
            }
            else return false;
        } catch(er) {
            return er.message;
        }
    }
    
    alert(fetchHeader(location.href,'Last-Modified'));
    
    0 讨论(0)
提交回复
热议问题