Check and download a text file on the client side

I am trying to upload a file to a browser and access it using JavaScript. Is it possible? I looked around and it seems that you can do this using flash. I was trying to figure out if there is an HTML5 / pure JavaScript solution.

I am trying to download a CSV file (each line contains a possible entry in the database) and check it on the fly using javascript. If it passes the test, I will send a POST to the server to create the items.

+3
source share
1 answer

It is possible. MDN offers a detailed explanation on this .

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>
+8

All Articles