将PhoneGap FileWriter.write用于“大”档 [英] Using PhoneGap FileWriter.write for "big" files

查看:114
本文介绍了将PhoneGap FileWriter.write用于“大”档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的PhoneGap应用中存在问题。我想写一个15 MB的文件。如果我尝试操作系统会吸引越来越多的内存,应用程序会在没有消息的情况下崩溃。我可以在Android和黑莓平板电脑上重现这一点。有没有办法更有效地实现写作?

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 on android and blackberry tablets. Is there a way to implement the writing more efficient?

最好的问候

fe.createWriter(
(fw: any) => {
    fw.onwriteend = (e) => {
        fw.onwriteend = (e) => {
            callback();
        }
        fw.write(data);
    }

    // write BOM (dead for now)
    fw.write("");
},
(error: any) => {
    alert("FileWriter Failed: " + error.code);
});

这是TypeScript,我希望JS开发人员不会为此而烦恼;)

It's TypeScript, I hope JS developers won't struggle with this ;)

推荐答案

我找到了答案。

崩溃原因:
PhoneGap FileWrite.write无法处理太大的缓冲区,不知道确切的大小,我认为
这个问题是由于PG通过URL方案将数据传输到iOS,不知怎的,当
URL过长时会崩溃。

Crash reason: PhoneGap FileWrite.write cannot handle too big buffer, do not know exact size, I think this issue is due to PG transfer data to iOS through URL Scheme, somehow it crash when "URL" is too long.

如何修复它:每次写小块,代码如下:

How to fix it: write small block every time, code below:

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) {
    var sz = Math.min(BLOCK_SIZE, data.byteLength - written);
    var sub = data.slice(written, written+sz);
    writer.write(sub);
    written += sz;
    writer.onwrite = function(evt) {
      if (written < data.byteLength)
        writeNext(cbFinish);
      else
        cbFinish();
    };
  }
  writeNext(writeFinish);
}

更新2014年8月12日:

UPDATE Aug 12,2014:

在我的实践中,通过Cordova FileSystem保存文件的性能不佳,特别是对于手机上的大文件(> 5M),需要几秒钟。如果您要将文件从服务器下载到本地磁盘,您可能需要高效且直接的方式,请尝试 cordova-plugin-file-transfer 插件。

In my practice, the performance of saving file through Cordova FileSystem is not good, especially for large file(>5M) on phone, it takes a few seconds. If you are downloading file from server to local disk, you may want a "efficient and direct" way, try cordova-plugin-file-transfer plugin.

这篇关于将PhoneGap FileWriter.write用于“大”档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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