[英]Read file from relative path as ArrayBuffer
I'm trying to read a local file into an ArrayBuffer using the FileReader API, like this我正在尝试使用 FileReader API 将本地文件读入 ArrayBuffer,就像这样
let reader = new FileReader();
reader.onload = function(e) {
let arrayBuffer = new Uint8Array(reader.result);
console.log(arrayBuffer);
}
reader.readAsArrayBuffer(new File([], 'data.txt'));
But I'm getting an empty arrayBuffer但我得到了一个空的 arrayBuffer
How can I read this local file as an ArrayBuffer in my browser?如何在浏览器中将此本地文件作为 ArrayBuffer 读取?
Thank you.谢谢你。
You cannot read a file by pathname through a browser.您无法通过浏览器按路径名读取文件。 You need to have the user interact with the file system and choose a file before you can read the content.您需要让用户与文件系统交互并选择一个文件,然后才能读取内容。
const readFile = e => { const file = e.target.files[0] let reader = new FileReader(); reader.onload = function(e) { let arrayBuffer = new Uint8Array(reader.result); console.log(arrayBuffer); } reader.readAsArrayBuffer(file); } document.querySelector("#fileItem").onchange=readFile
<input id="fileItem" type="file">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.