如何从JavaScript中的Txt文件中读取文本 [英] How Do I Read Text From Txt File In JavaScript
本文介绍了如何从JavaScript中的Txt文件中读取文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好,
我正在建立一些网页,我有一个问题。在我的HTML网页上有一个标签和一个按钮(我正在使用此标签<按钮>),按钮点击我应该调用一些javascript函数,它将读取counter.txt文件并将数据发送到标签..
喜欢这个:
in counter.txt我的号码是151
点击按钮
label.text = 151
谢谢!
Hello,
i am building right now some web page and i have one problem. In my HTML web page there is one label and one button ("i am using this tag <button>"), on button click i should call some javascript function that will read counter.txt file and send data to label..
Like this:
in counter.txt i have number 151
when click on button
label.text = 151
Thanks!
推荐答案
<script type="text/javascript">
function readSingleFile(evt) {
//Retrieve the first (and only!) File from the FileList object
var f = evt.target.files[0];
if (f) {
var r = new FileReader();
r.onload = function(e) {
var contents = e.target.result;
document.getElementById("lbl").innerHTML=contents;
};
r.readAsText(f);
} else {
alert("Failed to load file");
}
}
document.getElementById('fileinput').addEventListener('change', readSingleFile, true);
</script>
Try this, it will work.
这篇关于如何从JavaScript中的Txt文件中读取文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文