Using PhoneGap FileWriter.write for “big” files

前端 未结 2 1665
夕颜
夕颜 2021-02-04 07:17

I have a problem in my PhoneGap app. I would like to write a file of 15 MB. If I try the OS pulls more and more memory and the app crashes without message. I can reproduce this

2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-04 07:44

    @Imskull's answer is the correct one ... i just want to put in the one for Blob (make sure it is blob and not arraybuffer) which is updated based on the one on top ... what i also added was a line to assure myself i am adding to the end of file ... it is more than enough to make ur app stop crashing (on ios mainly :P )

    function gotFileWriter(writer) {
      function writeFinish() {
        // ... your done code here...
      }
    
      var written = 0;
      var BLOCK_SIZE = 1*1024*1024; // write 1M every time of write
      function writeNext(cbFinish) {
        writer.onwrite = function(evt) {
          if (written < data.size)
            writeNext(cbFinish);
          else
            cbFinish();
        };
        if (written) writer.seek(writer.length);
        writer.write(data.slice(written, written + Math.min(BLOCK_SIZE, data.size - written)));
        written += Math.min(BLOCK_SIZE, data.size - written);
      }
      writeNext(writeFinish);
    }
    

提交回复
热议问题