admin管理员组

文章数量:1404605

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?

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?

Share Improve this question asked Mar 17, 2015 at 18:00 Damien King-AcevedoDamien King-Acevedo 671 gold badge1 silver badge6 bronze badges 4
  • Could I refresh the file? instead of waiting for it to change, just update the graph periodically automatically, or do I need user input for that? – Damien King-Acevedo Commented Mar 17, 2015 at 18:03
  • You need user input for that. – Ginden Commented Mar 17, 2015 at 18:04
  • 1 are you building a web app? i think you can do this in javascript with a chrome app. – Jeff Commented Mar 17, 2015 at 18:06
  • A chrome app might work, thanks, I hadn't thought of that. – Damien King-Acevedo Commented Mar 17, 2015 at 18:13
Add a ment  | 

1 Answer 1

Reset to default 3

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

NodeJS Filesystem API docs: https://nodejs/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');
    }
});

本文标签: Can Javascript check a file for content changesStack Overflow