Reputation: 877
I used this article to read a CSV file using JavaScript. The code below works fine in Chrome and Firefox, but IE 11 throws the following error:
Object doesn't support property or method 'readAsBinaryString'
when calling:
reader.readAsBinaryString(blob);
According to this MSDN article, however, readAsBinaryString
is a method supported by FileReader
. Am I missing something / is there a different way of reading files in IE 11?
HTML code:
<table>
<tr>
<td>Import CSV File</td>
<td><input type="file" id="files" name="file"/></td>
</tr>
</table>
<button id="read" href="#">Read</button>
JS code:
//read a file
function readBlob() {
var files = document.getElementById('files').files;
if (!files.length) {
alert('Please select a file!');
return;
}
var file = files[0];
var start = 0;
var stop = file.size - 1;
var reader = new FileReader();
// If we use onloadend, we need to check the readyState.
reader.onloadend = function(evt) {
if (evt.target.readyState == FileReader.DONE) { // DONE == 2
makeJSON(evt.target.result);
}
};
var blob = file.slice(start, stop + 1);
reader.readAsBinaryString(blob);
}
Upvotes: 3
Views: 9519
Reputation: 877
FileReader
also supports readAsText
method, which can be used to read a File or Blob object into memory as a text string across all three browsers (Chrome, FF, and IE11).
Replacing reader.readAsBinaryString(blob)
with reader.readAsText(blob)
fixed the problem.
Upvotes: 6