admin管理员组

文章数量:1325361

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 acplish 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.

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 acplish 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.

Share Improve this question edited May 10, 2012 at 7:35 Rob W 349k87 gold badges807 silver badges682 bronze badges asked May 6, 2012 at 11:04 MarkMark 1,8572 gold badges13 silver badges6 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 10

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/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>

本文标签: javascriptClient side text file validation and uploadStack Overflow