Reputation: 1857
I'm trying to upload a file into the browser and access it using JavaScript. Is this possible? I've looked around and it seems that you can accomplish this using flash. I was trying to see if there was an HTML5/pure JavaScript solution.
I'm trying to upload a CSV file (each row contains a possible database entry) and validate it on the fly using javascript. If it passes validation, then I'll send a POST to the server to create the items.
Upvotes: 4
Views: 3211
Reputation: 348972
This is possible. MDN offers a detailed explanation on this.
The following is a basic method to read a text file using the FileReader
API:
http://jsfiddle.net/tGpDG/
<input type="file" id="file_upload">
<script>
var input_file = document.getElementById('file_upload');
input_file.onchange = function() {
var file = this.files[0];
// Do something with the FileReader object
var reader = new FileReader();
reader.onload = function(ev) {
// Show content (ev.target === reader)
alert(ev.target.result);
};
// Read as plain text
reader.readAsText(file);
};
</script>
Upvotes: 10