(Am new to web programming, so apologies for any lack of rudimentary knowledge.)
My page allows a user to select a file that is then read clientside & displayed in a textbox on the page. The easiest way I found to do this was to use a FileReader object, which works fine in Firefox and Chrome.
This doesn't work in Safari (yet), so what should I do instead?
//When the eventlistener detects a change in the input file...
var file = evt.target.files[0]
var reader = new FileReader();
reader.onload = function (e){document.getElementById('data').value = e.target.result};
reader.readAsText(file);
Relevant Notes:
The FileReader object lets web applications asynchronously read the contents of files (or raw data buffers) stored on the user's computer, using File or Blob objects to specify the file or data to read.
The FileReader result property returns the file's contents. This property is only valid after the read operation is complete, and the format of the data depends on which of the methods was used to initiate the read operation.
FileReader is an object with the sole purpose of reading data from Blob (and hence File too) objects. It delivers the data using events, as reading from disk may take time. The constructor: let reader = new FileReader(); // no arguments.
The FileReader. onload property contains an event handler executed when the load event is fired, when content read with readAsArrayBuffer, readAsBinaryString, readAsDataURL or readAsText is available.
Sadly, the only answer I can come up with will hog some extra bandwidth.
Firstly, use something like if (typeof FileReader !== "undefined"
or Modernizr to follow your normal flow for the browsers that DO support FileReader. Otherwise, POST the file via AJAX to some server-side script that echoes back the contents.
So for compliant browsers, you get to save yourself some bandwidth and for non-compliant browsers you have to take one for the team.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With