Click here to Skip to main content
15,895,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I tried writing a program to read the file contents of the file whose path we specify. kind of stuck with it..
<html>
<head>
<title>read_files</title>
</head>
<body>
<input type="file" id="fileinput" />
<script type="text/javascript">
function readSingleFile(evt) {
//Retrieve the first (and only!) File from the FileList object
var f = evt.target.files[0];
//document.getElementById("resultDiv").innerHTML=f;
//var eventdetails = "event name=" + evt.type + " target type= " + evt.target.type + " target tag name=" + evt.target.tagName;
//document.getElementById("resultDiv").innerHTML=eventdetails;

if (f) {
var r = new FileReader();
r.onload = function(e) {
var output = e.target.result;
//document.getElementById('results').innerHTML=output;
//process text to show only lines with "@":
output=output.split("\n").filter(/./.test, /\\/).join("\n");
document.getElementById('main').innerHTML= output;

}
r.readAsText(f);
} else {
alert("Failed to load file");
}
}

var path=document.getElementById('fileinput').addEventListener('change', readSingleFile, false);
//document.getElementById('resultDiv').innerHTML=path;
//alert(path.value);
</script>




</body>
</html>
this is the code ive written ignore the commented ones
Posted
Updated 3-Apr-15 8:46am
v2
Comments
Sergey Alexandrovich Kryukov 3-Apr-15 14:34pm    
Is it JavaScript embedded in a browser? Then is not supposed to access any file, for safety reasons; you only navigate to some URL and can read content from a page...
—SA
Member 11573324 3-Apr-15 14:48pm    
in this program i can access the file but ive to choose it manually
Sergey Alexandrovich Kryukov 3-Apr-15 15:03pm    
I was wrong: there is HTML5 File API: http://en.wikipedia.org/wiki/HTML5_File_API.
—SA
ZurdoDev 3-Apr-15 14:36pm    
You can't with JavaScript by itself. Click improve question and show what code you have that got you close.
Member 11573324 3-Apr-15 14:49pm    
I just did, I need to update this code such that instead of choosing manually it directly accesses the file from the path specified in the code

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900