TypedArray.prototype.slice() not supported on Chrome


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: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array" rel="nofollow">https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array</a>

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: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/slice" rel="nofollow">https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/slice</a>

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: <a href="http://jsperf.com/array-slice-vs-typedarray-subarray" rel="nofollow">http://jsperf.com/array-slice-vs-typedarray-subarray</a>

( However, the documentation for TypedArray.slice() and TypedArray.subarray() is slightly different: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array" rel="nofollow">https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array</a> )


  • Latest version of jQuery hotkeys plugin doesn't accept characters unless using keypress
  • How can I create a function that I could use to modify text displayed?
  • Display unicode character in Actionscript
  • JavaScript to allow only numbers, comma, dot, backspace
  • Is there any way to call saveCurrentTurnWithMatchData without sending a push notification?
  • Angular Bootstrap Carousel Slide Transition not working correctly
  • Getting error 'Cannot read property 'document' of undefined' while importing exp
  • Can I read an iPhone beacon with Windows.Devices.Bluetooth.Advertisement.BluetoothLEManufacturerData
  • Silverlight DependencyProperty.SetCurrentValue Equivalent
  • Reduction and collapse clauses in OMP have some confusing points
  • MongoDb aggregation
  • copying resource to sdcard gives a damaged file in android
  • How to use remove-erase idiom for removing empty vectors in a vector?
  • Spark fat jar to run multiple versions on YARN
  • Django: Count of Group Elements
  • Accessing IRQ description array within a module and displaying action names
  • Javascript Callbacks with Object constructor
  • How to check if every primary key value is being referenced as foreign key in another table
  • Where to put my custom functions in Wordpress?
  • How to handle AllServersUnavailable Exception
  • How to set my toolbar fixed while scrolling android
  • How to get next/previous record number?
  • AT Commands to Send SMS not working in Windows 8.1
  • retrieve vertices with no linked edge in arangodb
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • WPF Applying a trigger on binding failure
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Change div Background jquery
  • apache spark aggregate function using min value
  • Is it possible to post an object from jquery to bottle.py?
  • Easiest way to encapsulate a HTML5 webpage into an android app?
  • Busy indicator not showing up in wpf window [duplicate]
  • Sorting a 2D array using the second column C++
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal
  • How to push additional view controllers onto NavigationController but keep the TabBar?