我已经尝试使用javascript打开文本文件并获取他的名字和他的内容,所以现在我被困在字符串,因为我使用输入类型文件来获取目录/路径.
无论如何,我的问题是下一个代码有什么问题,我怎样才能使用javascript获取文本文件内容?
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Display Text Files</title>
<script type="text/javascript">
var str = document.getElementById('txt').value;
function display() {
if (str != "") {
var filename = str.split("/").pop();
document.getElementById('filename').innerHTML = filename;
}
}
</script>
</head>
<body>
<form method="get" action="#" >
<input type="file" accept="text/plain" id="txt" />
<input type="submit" value="Display Text File" onclick="display();" />
</form>
<a href="#" id="filename"></a>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
编辑:我也想在输入文件中禁用所有文件opition(*)到文本文件(.txt).
谢谢!