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 ?
Asked
Active
Viewed 543 times
0
-
5You can found an answere [Here](http://stackoverflow.com/questions/3582671/how-to-open-a-local-disk-file-with-javascript). – Jean-Charbel VANNIER Mar 25 '16 at 08:30
-
I have tried that already. But its not working... – Mar 25 '16 at 08:34
-
Did you check [browser compatibility](http://caniuse.com/#feat=fileapi) ? – Jean-Charbel VANNIER Mar 25 '16 at 08:36
1 Answers
0
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>

Community
- 1
- 1

STEAMworks Learning Center
- 1,167
- 1
- 8
- 20
-
Please do not copy answers from other questions. This question should be marked as a duplicate instead. – Philipp Wendler Mar 25 '16 at 09:05
-
-
Once you have more reputation, you can vote to mark questions as duplicate. For now, just leave a comment like Jean-Charbel VANNIER did. – Philipp Wendler Mar 25 '16 at 13:27