I am trying to read a text file using JavaScript and display the content in html file.
Its working as I have created the browse button to select the text file but I want to read the content from a fixed path file. In this program instead of taking the file path I want to read file from the path like - D:/new folder/abc.text
I am using the following code:
<html>
<input type="file" id="fileinput"/>
<script type="text/javascript">
function readSingleFile(evt) {
var f = evt.target.files[0];
if (f) {
var r = new FileReader();
r.onload = function(e) {
var contents = e.target.result;
document.write("the contents of the file are<br>");
document.write(contents);
}
r.readAsText(f);
} else {
alert("Failed to load file");
}
}
document.getElementById('fileinput').addEventListener('change',readSingleFile,false);
</script>
</html>
1 Answer 1
...but i want to read the content from a fixed path
You can't, not on a web browser, using standards, and accessing the local file system. There's a huge difference between allowing the browser to read a file the user has specifically identified for the page, and allowing it to read any file it wants. You simply cannot do the latter without resorting mechanisms (which will trigger security stuff) such as ActiveX, Flash, signed Java applets, and the like. The File API requires a file input element as a starting point for a reason.
4 Comments
input[type=file]. Mind you, they've updated the specification since the last time I had a really good look. But glancing through just now, it still looks like it relies on that input.