I am new to Javascript. I want to open a file from the localsystem only not from server ,when the user clicks on a single item from a list .So, I dont know how to open a file in javascript.so,can any one help me to figure it out ?
-
5You can found an answere Here.jevannie– jevannie2016-03-25 08:30:26 +00:00Commented Mar 25, 2016 at 8:30
-
I have tried that already. But its not working...user5124079– user51240792016-03-25 08:34:16 +00:00Commented Mar 25, 2016 at 8:34
-
Did you check browser compatibility ?jevannie– jevannie2016-03-25 08:36:04 +00:00Commented Mar 25, 2016 at 8:36
Add a comment
|
1 Answer
Here is an answer from @PaoloMoretti.
See this link:
How to Open Local Disk File With Javascript
Hope this helps,
Tim
CODE:
function readSingleFile(e) {
var file = e.target.files[0];
if (!file) {
return;
}
var reader = new FileReader();
reader.onload = function(e) {
var contents = e.target.result;
displayContents(contents);
};
reader.readAsText(file);
}
function displayContents(contents) {
var element = document.getElementById('file-content');
element.innerHTML = contents;
}
document.getElementById('file-input')
.addEventListener('change', readSingleFile, false);
<input type="file" id="file-input" />
<h3>Contents of the file:</h3>
<pre id="file-content"></pre>
3 Comments
Philipp Wendler
Please do not copy answers from other questions. This question should be marked as a duplicate instead.
STEAMworks Learning Center
Ok, no problem. How do I do that?
Philipp Wendler
Once you have more reputation, you can vote to mark questions as duplicate. For now, just leave a comment like Jean-Charbel VANNIER did.