我想知道是否有除了文件读取api和XMLHttprequest()
以外的任何方法来读取/加载支持几乎所有Web浏览器的文本文件。
我没有使用xmlhttprequest()
,因为它不能在几个浏览器中运行,比如chrome(因为它有一个bug)you may find it here 我想知道纯javascript/html方法,并希望避免使用php和django脚本。在html中阅读文件
我尝试以下方法XMLHttpRequest的
<!doctype html>
<html>
<head>
<body>
<script>
if (window.File && window.FileReader && window.FileList && winddow.Blob) { //alert('Great success! All the File APIs are supported.');
}
else {
alert('The File APIs are not fully supported in this browser.');
}
var req = new XMLHttpRequest();
req.open('GET', 'text.txt');
req.onreadystatechange = function() {
if (req.readyState == 4) {
if (req.status == 200) {
var lines = req.responseText.split(/\n/g);
lines.forEach(function(line, i) {
alert(line);
});
} else {
(alert("please update your browser,this webpage doesnt have support for your browser"))
}
}
}
req.send();
</script>
</body>
</head>
</html>
这将运行在Firefox 35.0.1
但是当我尝试在Chrome中使用它,它不工作
但是,如果我删除的条件if(req.status==200)
然后我收到一个没有文字的警报。
因此,运行chrome以允许文件访问。 – epascarello 2015-02-23 03:12:53
请向我们展示您到目前为止的代码。 – 2015-02-23 03:15:27
啊,着名的http状态码。 200:“确定”,不是200:“更新浏览器” – Winchestro 2015-02-23 06:23:49