我想修改this code,使其仅适用于特定文件,但是我无法找出正确的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>
最佳答案
由于安全性限制,浏览器没有提供这种功能。您不允许读取本地文件,除非用户不会在文件选择对话框中选择特定文件(或通过拖放操作不执行此操作)。这就是为什么所有代码示例都使用文件选择对话框的原因。
更多详细信息Does HTML5 allow you to interact with local client files from within a browser
关于javascript - 如何使用Javascript FileReader()打开本地文件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30596280/