Damien King-Acevedo
Damien King-Acevedo

Reputation: 67

Can Javascript check a file for content changes?

Example:

I have

var r = new FileReader();
    r.onload = function(e) {
        drawGraph(r.result);
    }
    r.readAsText(f);

drawing a graph from the file f input by the user.

Is there a way to check to see if the file f has been changed and then re load it's content without needing for the user to pick the file again?

Upvotes: 4

Views: 11260

Answers (1)

Jeff Voss
Jeff Voss

Reputation: 3695

Yes, this can be achieved using Node.js's filesystem API which provides a "watch" function

NodeJS Filesystem API docs: https://nodejs.org/api/fs.html

Similar question: Observe file changes with node.js

fs.watch('somedir', function (event, filename) {
    console.log('event is: ' + event);
    if (filename) {
        console.log('filename provided: ' + filename);
    } else {
        console.log('filename not provided');
    }
});

Upvotes: 3

Related Questions