[英]No data being passed through FileReader in Javascript
I'm expecting some text data (given I load a .txt file) to be shown in the <p>
element but nothing is actually passed. 我期望在
<p>
元素中显示一些文本数据(假设我加载了.txt文件),但实际上没有任何传递。
function showData() { var file = document.getElementById('myFile'); var data = file.files[0]; var fileRead = new FileReader(); fileRead.readAsDataURL(data); document.getElementById('out').appendChild(document.createTextNode(' ' + fileRead.result)); }
<p id = "out">The content of the file is</p> <input type = "file" id = "myFile" style="margin-top:5%;"> <button id = "show" onclick="showData()">The result is</button>
FileReader is asynchronous, therefore you have to set up a callback that will be invoked once the data has been read. FileReader是异步的,因此您必须设置一个回调,一旦读取数据就将调用该回调。 Also
readAsDataURL
is not appropriate here, you want readAsText
. 另外
readAsDataURL
在这里不合适,您需要readAsText
。
fileRead.onload = function() {
document.getElementById('out').appendChild(document.createTextNode(' ' + fileRead.result));
}
fileRead.readAsText(data);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.