答案不是真的,不。
但是有一些变通方法,取决于您可以摆脱什么(支持的浏览器等)
当您获取本地文件时(我假设您使用的是 <input type="file">,而不是部分支持的非标准化方法),您会收到一个要订阅的 "change" 事件,但该更改反映了所选内容的更改文件,而不是更改特定文件的内容。
除此之外,如果您有一个继承自 Blob 的 Blob 或 File,那么您基本上就有一个充满数据的缓冲区。
该缓冲区不会根据操作系统其他地方的变化动态更新自身,也不支持这样做的回调选项。
或者,对于 blob 或文件,您可以选择使用 URL.createObjectURL( file ),它会返回一个临时 url,它引用一个文件,而不是文件的内容。
也就是说,对该文件的任何更改都可能会反映在对该 URI 的后续调用中。
这不是我为测试而构建的东西。
不会有回调;但是,您可以设置基于间隔的心跳,对那个 URI 进行 XHR 调用,然后将 .responseText 与之前的 .responseText 进行比较。
同样,不能保证,但这与我认为您现在将得到的答案一样接近(虽然仍处于无插件/无扩展解决方案的领域),并且可能只值得花一个小时制定一个原型,以及更多测试支持(链接是否有效,而不是初始表示等)。
var input = document.querySelector("input[type='file']"), // client must open file, themselves
fileHandle = "", // this will be the url for the local file
content = "", // this will be the latest contents of the file
previousContent = ""; // this will be the previous contents
var ms = 1000,
secs = 2,
pollId; // will be the process ID for the polling (so you can stop checking for changes, later).
// wait for a file to be selected
input.addEventListener("change", function (evt) {
var file = evt.target.files[0]; // grab the file object
fileHandle = URL.createObjectURL(file); // create a url which points to the local file
// create an interval to check for changes
pollId = setInterval(
getLatest(fileHandle, getCurrent, updateCurrent),
secs * ms
);
});
function updateCurrent (latest) {
current = latest;
doStuff(current);
// the text has changed; it's been updated;
// now call home or do something else
}
function getCurrent () { return current; }
function getLatest (url, getCurrent, onchange) {
return function () {
// this isn't actually calling the network
// it's calling the local file
var xhr = new XMLHttpRequest();
xhr.open("GET", url);
xhr.onload = function () {
var latest = xhr.responseText;
if (getCurrent() !== latest) { onchange(latest); }
};
xhr.send();
};
}
您现在可以在支持的浏览器中进行更改轮询。