更新时间:2022-11-07 12:15:58
FileReader
requires the use of it's .onload property to trigger a callback when readAsArrayBuffer
(and other FileReader methods) has completed. The snippet below includes an example of how it could be used in tandem with the onChange event for a file input:
const input = document.getElementById('userFile');
const reader = new FileReader();
input.onchange = function() {
const file = input.files[0];
reader.readAsArrayBuffer(file);
};
reader.onload = function() {
const resultArray = new Int8Array(reader.result);
document.getElementById('result').innerHTML = resultArray;
};
<input type="file" id="userFile"/>
<h4>File Data:</h4>
<span id="result"/>