输入文件大小和内容不会在macOS上更新 [英] Input file size and content do not update on macOS

查看:111
本文介绍了输入文件大小和内容不会在macOS上更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个基于Web的小工具,它使用文件输入来读取不断变化的文件。用户手动选择它(一次!)和更改它时的JavaScript跟踪(上次文件修改时间和文件大小)。如果它已经改变,它会再次读取文件内容。

I wrote a small web based tool, which uses a file input to read a constantly changing file. The user selects it manually (once!) and JavaScript tracks when it was changed (last file modification time and file size). If it has changed, it reads the file contents again.

这适用于Windows上的所有浏览器。但是在macOS上(在Safari 10.1.2和Firefox 51.0.1中测试),只有最后修改时间似乎会更新。文件大小未更新,似乎也无法再读取文件内容。所以我无法跟踪macOS上浏览器中的文件更改。

This works fine in all browsers on Windows. But on macOS (tested in Safari 10.1.2 and Firefox 51.0.1) only the last modification time seems to be updated. The file size is not updated and it seems, that the file contents cannot be read anymore too. So I can not track file changes in browsers on macOS.

但为什么呢?这是macOS中的安全限制吗?

But why? Is this a security limitation in macOS?

请使用以下代码段进行测试。选择一个文件(例如文本文件),查看上次修改的时间戳和文件大小,然后更改文件并再次查看,如果大小已更改。在macOS上,文件大小不会改变。

Please test with following snippet. Select a file (for example a text file), see last modified timestamp and file size, then change file and look again, if size has changed. On macOS the file size doesn't change.

请不要jQuery。

window.addEventListener('load', function() {
  window.setInterval(function() {
    var logFile = document.querySelector('#file').files[0];
    if (logFile) {
      document.querySelector('#info').innerHTML = '<br/>' +
        (new Date()).toString() + '<br/>Last modified: ' +
        logFile.lastModified +
        '<br/>Size: ' +
        logFile.size;
    }
  }, 1000);
});

#info {
  font-family: Courier;
  font-size: 0.9em;
}

<!DOCTYPE html>
<input type="file" id="file" />
<p id="info"></p>

推荐答案

我不认为这是一项安全措施,而不仅仅是一项绩效衡量指标。

一旦他们获得了文件元数据信息,他们将不再要求进一步获取用户磁盘的权限。>
说实话,我甚至有点惊讶在Windows上,他们每次都会请求这些元数据。

I don't think it is a security measure rather than simply a performance measure.
Once they've got the file metadata info, they won't request it anymore on further getting => less access to user's disk.
To be honest, I am even a bit surprised that on Windows they do request these metadata every time.

要解决这个问题,这不是一项简单的任务,我会重新考虑对此的需求,如果消息可以'可以在另一个级别完成(例如从修改文件的过程)。

To workaround this, is not an easy task, and I would reconsider the needs for this, and if the messaging can't be done at another level (e.g from th process that does modify the files).

事实上,在这种情况下,Firefox和Safari的行为都不一样:

Indeed, both Firefox and Safari don't behave the same in this situation:


  • Safari似乎创建了一个指向文件的blobURI它会形成输入,并在您稍后尝试访问它时始终使用它(例如从FileReader)。这里真正的问题是我们无法在磁盘上获得新版本,因为无论出于何种原因,这个blobURI似乎不是一个真正的指针到磁盘...但是如果我们只想检测文件更改,这对我们有好处,因为我们只需检查 FileReader.onerror 事件,即使使用<$ c $也可以c> File.slice(0,1)(即最小I / O)。

  • Safari seems to create a blobURI pointing to the File as soon as you've picked it form the input, and always uses it when you try to access it later (e.g from a FileReader). The real problem here is that we can't get the new version on disk, because, for whatever reasons, this blobURI doens't seem to be a real pointer to the disk... But if we only want to detect file changes, then it's good for us, since we would only have to check for a FileReader.onerror event, which would work even with a File.slice(0,1) (i.e minimal I/O).

另一方面Firefox会触发如果缓存的元数据的大小与已读取的元数据的大小不匹配,则FileReader的错误。这意味着您必须在每次检查时阅读整个文件,如果没有发生错误,请仔细检查数据是否实际相同...... ,在此浏览器中,您仍然可以使用blobURI从AJAX获取磁盘上的实际文件,以获取最新版本。

Firefox on the other hand will trigger the FileReader's error only if the size of the cached metadata doesn't match with the one that has been read. This means that you would have to read the whole File at every check, and, if no error occured, double check that the data are actually the same... But, in this browser, you could still fetch the actual File on disk from AJAX using a blobURI, in order to get the latest version.

这篇关于输入文件大小和内容不会在macOS上更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆