【发布时间】:2016-01-04 04:56:33
【问题描述】:
我正在使用 File API 上传文件并将其内容保存为 JavaScript 中的变量。这是我目前所拥有的,但我得到的结果是undefined。
<body>
<input type="file" id="file" name="file" multiple />
<script>
function handleFileSelect(evt) {
// grab the file that was uploaded which is type File.
// evt is the event that was triggered
// evt.target returns the element that triggered the event
// evt.target.files[0] returns the file that was uploaded
var file = evt.target.files[0];
// instantiate a FileReader object to read/save the file that was uploaded
var reader = new FileReader();
// read the file and save as an array
fileArray = reader.readAsArrayBuffer(file);
window.alert("hello");
window.alert(fileArray);
}
document.getElementById('file').addEventListener('change', handleFileSelect, false);
</script>
【问题讨论】:
标签: javascript html fileapi