Javascript - Open A File From Localdisk
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 ja
Solution 1:
Here is an answer from @PaoloMoretti.
See this link:
How to Open Local Disk File With Javascript
Hope this helps,
Tim
CODE:
functionreadSingleFile(e) {
var file = e.target.files[0];
if (!file) {
return;
}
var reader = newFileReader();
reader.onload = function(e) {
var contents = e.target.result;
displayContents(contents);
};
reader.readAsText(file);
}
functiondisplayContents(contents) {
var element = document.getElementById('file-content');
element.innerHTML = contents;
}
document.getElementById('file-input')
.addEventListener('change', readSingleFile, false);
<inputtype="file"id="file-input" /><h3>Contents of the file:</h3><preid="file-content"></pre>
Post a Comment for "Javascript - Open A File From Localdisk"