gpt4 book ai didi

javascript - 自动上传本地文件,即使它在 Firefox 中发生更改

转载 作者:行者123 更新时间:2023-12-03 00:13:16 25 4
gpt4 key购买 nike

我正在寻找一种解决方案,允许浏览器持续上传用户最初选择的本地文件,即使该文件已被编辑。我有一个适用于 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 中是“事件的” - 更改文件长度,再次读取时 fileObject.length 的值会发生变化,读取文件的 lastModified 属性返回最多最近的修改日期和时间,读取文件内容会返回更新的内容(如帖子中所述)。

文件对象目前在 Firefox 中存在。即使文件长度已更改,读取 fileObject.length 返回的值也保持不变,并且读取文件的 lastModified 属性会返回创建文件引用时的时间戳。尝试读取更新的文件会导致文件读取器的 onerror 处理程序被调用,并以“NotReadableError:无法读取文件”作为参数。

File API specification截至 2019 年 2 月 1 日,仍是编辑草稿。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 的行为以“兼容”。同时,如果 Firefox 在未来某个时候采纳该提案草案,或者选择不采纳,我也不会感到惊讶。

总之,我很怀疑目前是否有 Firefox 解决方案能够满足您的要求。

关于javascript - 自动上传本地文件,即使它在 Firefox 中发生更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54627538/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com