自动上传本地文件,即使它在 Firefox 中发生了变化

Automatically upload local file even if it's changed in Firefox

我正在寻找一种解决方案,允许浏览器持续上传用户最初选择的本地文件,即使该文件已被编辑。我有一个适用于 chrome 的解决方案,但一旦文件被编辑,Firefox 就会停止上传它。该代码粘贴在下面。

function readSingleFile(e) {
  var readers = new Array();
  setInterval(function () {
  var file = e.target.files[0];
  readers.push(new FileReader());
  readers[readers.length-1].onload = function(e) {
    var contents = e.target.result;
    // Display file content
    displayContents(contents);
    console.log(contents);
  };
  readers[readers.length-1].readAsText(file);
}, 5000);
}

function displayContents(contents) {
  var element = document.getElementById('file-content');
  element.innerHTML += contents;
}

document.getElementById('file-input').addEventListener('change', readSingleFile, false);

文件对象在Chrome中是"live"-改变文件长度,再次读取时fileObject.length的值改变,读取文件的lastModified属性 returns最近的修改日期和时间,以及读取文件的内容returns更新的内容(如post中所述)。

文件对象目前存在于 Firefox 中。即使文件长度已更改,读取 fileObject.length 返回的值也保持不变,读取文件的 lastModified 属性 returns 是创建文件引用时的时间戳。尝试读取更新的文件会导致文件 reader 的 onerror 处理程序以 "NotReadableError: File could not be read" 作为参数被调用。

File API specification is still an editors draft at Feb 1 2019. The File Interface section 谈到对磁盘上的文件状态(名称、长度和最后修改日期)进行快照:

If a File object is a reference to a byte sequence originating from a file on disk, then its snapshot state should be set to the state of the file on disk at the time the File object is created.

并在其后加上以下注释(强调我的):

Note: This is a non-trivial requirement to implement for user agents, and is thus not a must but a should [RFC2119]. User agents should endeavor to have a File object’s snapshot state set to the state of the underlying storage on disk at the time the reference is taken. If the file is modified on disk following the time a reference has been taken, the File's snapshot state will differ from the state of the underlying storage. User agents may use modification time stamps and other mechanisms to maintain snapshot state, but this is left as an implementation detail.

我的理解是,在标准开发的这个阶段,Firefox 不需要将 Chrome 的行为实现为 "compliant"。同时,如果 Firefox 在未来的某个时候采纳提案草案,或者选择不采纳,我也不会感到惊讶。

总而言之,我很怀疑 目前是否有满足您需求的 Firefox 解决方案。