How To Read Local Files Using Html 5 Filereader?
Objective I am making an application, and I need to read a local file using JavaScript and HTML 5, without any tags or user interaction whatsoever. What I tried On my
Solution 1:
The HTML5 fileReader facility does allow you to process local files, but these MUST be selected by the user, you cannot go rooting about the users disk looking for files.
Is it possible to load a file with JS/HTML5 FileReader on non served page?
How to open a local disk file with Javascript?
How to set a value to a file input in HTML?
Javascript read file without using input
These links help you to find answer.
Solution 2:
This Can do a trick.
HTML
<h1>Text File Reader</h1><div>
Select a text file:
<inputtype="file"id="fileInput"></div><preid="fileDisplayArea"><pre></div>
JS
window.onload = function() {
var fileInput = document.getElementById('fileInput');
var fileDisplayArea = document.getElementById('fileDisplayArea');
fileInput.addEventListener('change', function(e) {
var file = fileInput.files[0];
var textType = /text.*/;
if (file.type.match(textType)) {
var reader = newFileReader();
reader.onload = function(e) {
fileDisplayArea.innerText = reader.result;
}
reader.readAsText(file);
} else {
fileDisplayArea.innerText = "File not supported!"
}
});
}
Post a Comment for "How To Read Local Files Using Html 5 Filereader?"