我想修改此代码,使其仅适用于特定文件,但我无法确定正确的 URL 参数,并且我找到的所有代码示例都使用文件选择对话框。
<!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>
为什么当我从以下位置更改代码时它不起作用:
<body>
<input type="file" onchange='readText(this)' />
<div id="main"></div>
</body>
至:
<body onload="readText('file:///C:/test.txt')">
<div id="main"></div>
</body>