Check if file has changed using HTML5 File API

后端 未结 3 1997
生来不讨喜
生来不讨喜 2020-11-30 03:46

Okay, so I have a program which outputs some specific data to a tab separated variable file.

I had been using Excel to open and view the file contents, however I fou

相关标签:
3条回答
  • 2020-11-30 03:56

    While T.J. Crowder's answer is correct, Chrome's implementation appears to break the spec.

    Each Blob must have an internal snapshot state, which must be initially set to the state of the underlying storage, if any such underlying storage exists, and must be preserved through structured clone. Further normative definition of snapshot state can be found for files.

    When a file is selected the input has a snapshot of the contents at that point. Local changes on disk don't update the snapshot.

    0 讨论(0)
  • 2020-11-30 04:09

    I don't believe the File API has any event for the file changing, just progress events and the like.

    Update August 2020: The alternative below no longer works, and the specification specifically disallows it by saying that the File object's information must reflect the state of the underlying file as of when it was selected. From the spec:

    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.


    You could use polling. Remember the lastModifiedDate of the File, and then when your polling function fires, get a new File instance for the input and see if the lastModifiedDate has changed.

    This works for me on Chrome, for instance: Live Copy | Source

    (function() {
      var input;
      var lastMod;
    
      document.getElementById('btnStart').onclick = function() {
        startWatching();
      };
        function startWatching() {
            var file;
    
            if (typeof window.FileReader !== 'function') {
                display("The file API isn't supported on this browser yet.");
                return;
            }
    
            input = document.getElementById('filename');
            if (!input) {
                display("Um, couldn't find the filename element.");
            }
            else if (!input.files) {
                display("This browser doesn't seem to support the `files` property of file inputs.");
            }
            else if (!input.files[0]) {
                display("Please select a file before clicking 'Show Size'");
            }
            else {
                file = input.files[0];
          lastMod = file.lastModifiedDate;
                display("Last modified date: " + lastMod);
          display("Change the file");
          setInterval(tick, 250);
            }
        }
    
      function tick() {
        var file = input.files && input.files[0];
        if (file && lastMod && file.lastModifiedDate.getTime() !== lastMod.getTime()) {
          lastMod = file.lastModifiedDate;
                display("File changed: " + lastMod);
        }
      }
    
      function display(msg) {
        var p = document.createElement('p');
        p.innerHTML = msg;
        document.body.appendChild(p);
      }
    })();
    <input type='file' id='filename'>
    <input type='button' id='btnStart' value='Start'>

    0 讨论(0)
  • 2020-11-30 04:09

    There is two solutions to this problem, and <input type="file"> is not one of them. according to the spec, it creates a "snapshot" of the file.


    Native File System

    This api is experimental and requires flags to be enabled in blink (aka chromium browsers). The idea is that you get a file handle and when you need the file then you call the async "getFile" function to retrieve the actual file.

    this feature is a "power feature" and require your site to be secure, and it can't work in sandboxed iframes

    So without testing here is some "code in the dark":

    // triggerd on click
    async function pickFile () {
      // const handle = showOpenFilePicker() // chrome 85
      const handle = await chooseFileSystemEntries() // deprecated (split up to 3 fns)
        let lastModificationTime = new Date(0)
      
      async function compare (meta) {
        const file = await handle.getFile()
        if (file.lastModifiedDate > lastModificationTime) {
          lastModificationTime = file.lastModifiedDate
          console.log(await file.text())
        }
      }
      
      setInterval(compare, 1000)
    }
    

    Get Entry from Drag and drop

    Similar to the native file system you can also retrieve a file handle and do the same thing, but this feature works in browsers today. but this code snippet don't work in stackoverflow since it use some sandboxing do making it inkompatible, so here is a fiddle with few comments

    function drop(event) {
      event.stopPropagation();
      event.preventDefault();
        
      // get the file as an fileEntry (aka file handle)
      const fileEntry = event.dataTransfer.items[0].webkitGetAsEntry()
      let lastModificationTime = new Date(0)
      
      async function read (file) {
        // use the new async read method on blobs.
        console.log(await file.text())
      }
      
      function compare (meta) {
        if (meta.modificationTime > lastModificationTime) {
          lastModificationTime = meta.modificationTime
          fileEntry.file(read)
        }
      }
      
      setInterval(fileEntry.getMetadata.bind(fileEntry, compare), 1000)
    }
    
    0 讨论(0)
提交回复
热议问题