23

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 it possible to retrieve the last modified date of a PDF or DOC file using Javascript? I don't have any specific need to use Javascript, but it is preferable.

UPDATE: Now that I realize that Javascript can't access the filesystem, is there an alternative method?

Riddari
  • 1,713
  • 3
  • 26
  • 57

7 Answers7

36

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'));
arboc7
  • 5,762
  • 2
  • 27
  • 30
kennebec
  • 102,654
  • 32
  • 106
  • 127
8

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.

j-p
  • 3,698
  • 9
  • 50
  • 93
  • 7
    This gives last modified of the `current document`. kennebec's solution gives last modified of an `arbitrary file` you have the URL to. – Jesse Chisholm Jan 29 '19 at 00:25
5

Using the modern fetch method:

var lastMod = null;
fetch(xmlPath).then(r => {
    lastMod = r.headers.get('Last-Modified');
    return r.text();
})
FutureBoy
  • 66
  • 1
  • 1
  • I had to put this in an `async` method, so I had to `await fetch(xmlPath)...`, and also move the return statement outside of the `fetch()`: `return lastMod` – Paul Jul 20 '21 at 16:33
  • Credit to @FutureBoy--reformatted into a separate [answer](https://stackoverflow.com/a/74967740/1356991) due to comment limitations on code blocks. – CrazyIvan1974 Dec 31 '22 at 05:43
1

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}`);
Grant Miller
  • 27,532
  • 16
  • 147
  • 165
1

Formatting FutureBoy's answer as a full function and adding a HEAD method and date conversion, the code would appear as follows.

function fetchLastModified(url, callback) {
    fetch(url, {method: "HEAD"})
        .then(r => {callback(new Date(r.headers.get('Last-Modified')))});
}

HEAD reduces the amount of data transmitted to just include the HTTP headers. Since FutureBoy's answer just used the headers, I wrote the function to only pull the headers. See Mozilla.org's HEAD documentation.

CrazyIvan1974
  • 377
  • 1
  • 2
  • 11
0

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.

jldupont
  • 93,734
  • 56
  • 203
  • 318
-2

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

Naeem Sarfraz
  • 7,360
  • 5
  • 37
  • 63