Is it possible to clean memory after FileReader?

后端 未结 2 1825
臣服心动
臣服心动 2021-02-04 10:52

FileReader seems to consume all the memory as it is repeatedly used to preload multiple blobs, and never frees it. Any known way to force it to release consumed memory? Setting

2条回答
  •  北海茫月
    2021-02-04 11:41

    Try it like this instead:

    function sliceMe() {
            var file = document.getElementById('file').files[0],
            fr = new FileReader,
            chunkSize = 2097152,
            chunks = Math.ceil(file.size / chunkSize),
            chunk = 0;
    
        function loadNext() {
           var start, end,
               blobSlice = File.prototype.mozSlice || File.prototype.webkitSlice;
    
           start = chunk * chunkSize;
           end = start + chunkSize >= file.size ? file.size : start + chunkSize;
    
           fr.onload = function() {      
              if (++chunk < chunks) {
                 //console.info(chunk);
              }
           };
           fr.onloadend = function(e) {      
              loadNext(); // shortcut here
           };
           fr.readAsBinaryString(blobSlice.call(file, start, end));
        }
    
        loadNext();
    }
    

    The onloadend will keep you from overlapping your other reads... (Obviously, you can fix the increment a little better, but you get the idea...)

提交回复
热议问题