【发布时间】:2011-08-10 06:52:14
【问题描述】:
我尝试使用从http://www.html5rocks.com/tutorials/file/dndfiles/ 修改的以下代码来读取文本或 xml 文件并显示下面的内容。
<!DOCTYPE html>
<html>
<head>
<title>reading xml</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<input type="file" id="files" name="files[]" multiple />
<output id="list"></output>
<script>
function handleFileSelect(evt) {
var files = evt.target.files; // FileList object
// Loop through the FileList
for (var i = 0, f; f = files[i]; i++) {
var reader = new FileReader();
// Closure to capture the file information.
reader.onload = (function(theFile) {
return function(e) {
// Print the contents of the file
var span = document.createElement('span');
span.innerHTML = ['<p>',e.target.result,'</p>'].join('');
document.getElementById('list').insertBefore(span, null);
};
})(f);
// Read in the file
//reader.readAsDataText(f,UTF-8);
reader.readAsDataURL(f);
}
}
document.getElementById('files').addEventListener('change', handleFileSelect, false);
</script>
</body>
reader.readAsDataText(f,UTF-8);不工作
reader.readAsDataURL(f);以 Base64 显示文件
如何获取要在页面上显示的文本文件?
【问题讨论】:
标签: javascript file html