在cordova-plugin-file(iOS)的FileReader上调用readAsArrayBuffer方法时出现内存不足错误 [英] out of memory error when calling readAsArrayBuffer method on FileReader of the cordova-plugin-file (iOS)

查看:71
本文介绍了在cordova-plugin-file(iOS)的FileReader上调用readAsArrayBuffer方法时出现内存不足错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在iOS上,我尝试将视频上传到我自己的网站上,为此,我使用FileReader.readAsArrayBuffer方法,chunkSize为1048576.

On iOS I'm trying to upload videos to my own website and to do so I use the FileReader.readAsArrayBuffer method with a chunkSize of 1048576.

当onprogress事件触发时,我上传了块,除了较大的文件以外,所有这些实际上都可以正常运行.尝试上传1.33GB的文件时,调用readAsArrayBuffer方法时遇到内存不足异常.

I upload chunks when the onprogress event fires and all of this actually works perfect except for bigger files. When trying to upload a file of 1.33GB i'm getting a out of memory exception when calling the readAsArrayBuffer method.

我猜这是因为它正在尝试为整个文件保留内存,但这不是必需的.有没有一种方法可以在不为整个文件保留内存的情况下从文件中读取二进制块?还是有其他解决方案?

I guess this is because it's trying to reserve memory for the complete file, however this is not necessary. Is there a way to read a binary chunk from a file without it reserving memory for the complete file? or are there other solutions?

谢谢!

推荐答案

我今天通过更改插件代码来修复了它,这是原始代码:

I fixed it myself today by changing the plugin code, this is the original code:

FileReader.prototype.readAsArrayBuffer = function (file) {
   if (initRead(this, file)) {
       return this._realReader.readAsArrayBuffer(file);
   }

   var totalSize = file.end - file.start;
   readSuccessCallback.bind(this)('readAsArrayBuffer', null, file.start, totalSize, function (r) {
       var resultArray = (this._progress === 0 ? new Uint8Array(totalSize) : new Uint8Array(this._result));
       resultArray.set(new Uint8Array(r), this._progress);
       this._result = resultArray.buffer;
   }.bind(this));
};

并且由于在开始时进度始终为0,因此它将始终保留整个文件大小.我添加了一个属性READ_CHUNKED(因为我仍然有其他现有代码也使用此方法,并且希望它能像以前一样工作,因此我必须检查其他所有内容是否仍在工作),并将以上内容更改为:

and since at start progress is always 0, it always reserves the entire filesize. I added a propery READ_CHUNKED (because I still have other existing code that also uses this method and expects it to work as it did, i have to check to see that everything else also keeps working) and changed the above to this:

FileReader.prototype.readAsArrayBuffer = function(file) {
    if (initRead(this, file)) {
        return this._realReader.readAsArrayBuffer(file);
    }

    var totalSize = file.end - file.start;

    readSuccessCallback.bind(this)('readAsArrayBuffer', null, file.start, totalSize, function(r) {
        var resultArray;

        if (!this.READ_CHUNKED) {
            resultArray = new Uint8Array(totalSize);
            resultArray.set(new Uint8Array(r), this._progress);
        } else {
            var newSize = FileReader.READ_CHUNK_SIZE;
            if ((totalSize - this._progress) < FileReader.READ_CHUNK_SIZE) {
                newSize = (totalSize - this._progress);
            }
            resultArray = new Uint8Array(newSize);
            resultArray.set(new Uint8Array(r), 0);
        }
        this._result = resultArray.buffer;
    }.bind(this));
};

当READ_CHUNKED属性为true时,它仅返回块,并且不为整个文件保留内存;当它为false时,它的工作方式与以前一样.

When the READ_CHUNKED property is true, it only returns the chunks and doesn't reserve memory for the entire file and when it is false it works like it used to.

我从没使用过github(除了拉动代码外),所以我暂时不上传它,我可能会在不久的将来进行研究.

I've never used github (except for pulling code) so i'm not uploading this for now, I might look into it in the near future.

这篇关于在cordova-plugin-file(iOS)的FileReader上调用readAsArrayBuffer方法时出现内存不足错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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