Chrome上不支持TypedArray.prototype.slice() [英] TypedArray.prototype.slice() not supported on Chrome

查看:173
本文介绍了Chrome上不支持TypedArray.prototype.slice()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想上传大文件,使用Uint8Array和slice()函数。
slice()是必须的,因为大文件也必须被处理。
$ b $ $ $ $ $ $ $ $ $ $ var fileReader = new FileReader );

fileReader.onloadend = function(event){
var contents = new Uint8Array(event.target.result);

var bufferSize = 8192;
var byteBuffer = [];

var temp = null;
var pos = 0;
for(var i = 0; i< contents.length; i + = bufferSize){
pos = contents.length>我+ bufferSize? i + bufferSize:contents.length;
byteBuffer.push(String.fromCharCode.apply(null,contents.slice(i,pos)));
}

var bytes = byteBuffer.join('');

contents = undefined;
byteBuffer = undefined;

var formData = new FormData();
formData.append('name','somefile.dat');
formData.append('data',bytes);

//做POST formData

};上面的代码只能在Firefox中使用。







<所有浏览器都支持Uint8Array:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array



问题是Uint8Array是从TypedArray继承的,上面的代码使用TypedArray.prototype.slice()函数。这仅在Firefox中受支持: https:// developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/slice

还有一个子数组()函数,但是这不会创建一个浅的副本。在处理大文件时,创建深层副本不是一个好主意。



我也查看了lodash的slice(),但是它是针对Array而不是TypedArray的。所以这不适合我。



也许我应该写一个函数来创建子数组的浅表副本

子数组()来代替 slice()

这两个工作在这里是一样的,也可能是调用相同的内部实现。
查看使用Firefox的基准:
http://jsperf.com / array-slice-vs-typedarray-subarray



(但是, TypedArray.slice() TypedArray.subarray()略有不同:
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array


I would like to upload large files, using Uint8Array and slice() function. The slice() is needed, because large files have to be handled too.

var fileReader = new FileReader();

fileReader.onloadend = function(event) {
    var contents = new Uint8Array(event.target.result);

    var bufferSize = 8192;
    var byteBuffer = [];

    var temp = null;
    var pos = 0;
    for(var i = 0; i < contents.length; i+=bufferSize) {
        pos =  contents.length > i+bufferSize ? i+bufferSize : contents.length;
        byteBuffer.push(String.fromCharCode.apply(null, contents.slice(i, pos)));
    }

    var bytes = byteBuffer.join('');

    contents = undefined;
    byteBuffer = undefined;

    var formData = new FormData();
    formData.append('name', 'somefile.dat');
    formData.append('data', bytes);

    // do the POST formData

};

The code above works only in Firefox.

The Uint8Array is supported on all browsers: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array

The problem is the Uint8Array is inherited from TypedArray, and the code above uses the TypedArray.prototype.slice() function. Which is only supported in Firefox: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/slice

There is a subarray() function too, but that doesn't create a shallow copy. While processing large files, it's not a good idea to create deep copy.

I looked at the lodash's slice() too, but it's for Array and not TypedArray. So that is not working for me.

Maybe I should write a function to create a shallow copy of the subarray?

解决方案

Just use subarray() in place of slice().

The two works the same here, and it might be calling the same internal implementation. See the benchmarks using Firefox: http://jsperf.com/array-slice-vs-typedarray-subarray

( However, the documentation for TypedArray.slice() and TypedArray.subarray() is slightly different: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array )

这篇关于Chrome上不支持TypedArray.prototype.slice()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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