Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
142
rated 0 times [  149] [ 7]  / answers: 1 / hits: 37310  / 9 Years ago, tue, june 2, 2015, 12:00:00

I would like to modify this code so that it works with a specific file only, but I can't figure out the correct URL parameter and all the code examples that I've found use a file selection dialog.



<!DOCTYPE html>
<html>
<head>
<title>reading file</title>
<script type=text/javascript>

var reader = new FileReader();

function readText(that){

if(that.files && that.files[0]){
var reader = new FileReader();
reader.onload = function (e) {
var output=e.target.result;

//process text to show only lines with @:
output=output.split(n).filter(/./.test, /@/).join(n);

document.getElementById('main').innerHTML= output;
};//end onload()
reader.readAsText(that.files[0]);
}//end if html5 filelist support
}
</script>
</head>
<body>
<input type=file onchange='readText(this)' />
<div id=main></div>
</body>


Why doesn't it work when I change the code from:



<body>
<input type=file onchange='readText(this)' />
<div id=main></div>
</body>


to:



<body onload=readText('file:///C:/test.txt')>
<div id=main></div>
</body>

More From » filereader

 Answers
91

Browsers don't give such ability because of security restrictions. You're not allowed to read local files, untill user won't select specific file in file selection dialog (or won't do this with drag-n-drop). That's why all code examples use file selection dialog.



More details Does HTML5 allow you to interact with local client files from within a browser


[#66371] Saturday, May 30, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
alorac

Total Points: 262
Total Questions: 82
Total Answers: 97

Location: Libya
Member since Mon, Dec 7, 2020
4 Years ago
alorac questions
Sat, Oct 10, 20, 00:00, 4 Years ago
Tue, Sep 22, 20, 00:00, 4 Years ago
Wed, Jul 1, 20, 00:00, 4 Years ago
Wed, Jun 3, 20, 00:00, 4 Years ago
Sun, May 17, 20, 00:00, 4 Years ago
;