Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript readAsBinaryString Function on E11

In this page http://www.html5rocks.com/en/tutorials/file/dndfiles/ if you scroll down to example "Example: Slicing a file. Try it!" you will see uses of readAsBinaryString API to read bytes of local files.

I've seen IE (My case its IE11) doesn't support readAsBinaryString.

Even this code mentioned in post HTML5 File API read as text and binary breaks at readAsBinaryString in IE11.

I have seen some post in stack overflow, it suggests use of ReadAsArrayBuffer(). But it is also not working. It returns undefined.

My question is what are the options if I have to run it on IE11? Is it possible to write another IE compatible JS function which will do the JOB of readAsBinaryString().

like image 223
Dev.K. Avatar asked Jul 13 '15 18:07

Dev.K.


2 Answers

I combine @Jack answer with my comment to show a complete working example.

In the <head> section I added this script to add FileReader.readAsBinaryString function in IE11

if (FileReader.prototype.readAsBinaryString === undefined) {     FileReader.prototype.readAsBinaryString = function (fileData) {         var binary = "";         var pt = this;         var reader = new FileReader();         reader.onload = function (e) {             var bytes = new Uint8Array(reader.result);             var length = bytes.byteLength;             for (var i = 0; i < length; i++) {                 binary += String.fromCharCode(bytes[i]);             }             //pt.result  - readonly so assign content to another property             pt.content = binary;             pt.onload(); // thanks to @Denis comment         }         reader.readAsArrayBuffer(fileData);     } } 

Then I needed to slightly modify my original script code because target.result has no value when using this fallback function.

var reader = new FileReader(); reader.onload = function (e) {     // ADDED CODE     if (!e) {         var data = reader.content;     }     else {         var data = e.target.result;     }      // business code }; reader.readAsBinaryString(myFile); 
like image 149
Naigel Avatar answered Sep 28 '22 05:09

Naigel


This is my solution.

var reader = new FileReader(); reader.readAsBinaryString(fileData); reader.onload = function(e) {   if (reader.result) reader.content = reader.result;   var base64Data = btoa(reader.content);   //... } //extend FileReader if (!FileReader.prototype.readAsBinaryString) {     FileReader.prototype.readAsBinaryString = function (fileData) {        var binary = "";        var pt = this;        var reader = new FileReader();              reader.onload = function (e) {            var bytes = new Uint8Array(reader.result);            var length = bytes.byteLength;            for (var i = 0; i < length; i++) {                binary += String.fromCharCode(bytes[i]);            }         //pt.result  - readonly so assign binary         pt.content = binary;         $(pt).trigger('onload');     }     reader.readAsArrayBuffer(fileData);     } } 
like image 21
Jack Avatar answered Sep 28 '22 05:09

Jack