我目前正在尝试将文件上传到我的服务器。但我不太确定如何使用 readAsArrayBuffer 执行此操作。如果我使用 readAsBinaryString,这会起作用。
如果我尝试 console.log,它只会返回 'arrayBuffer: {}'。 在我尝试上传它之后,我可以在帖子中看到只发送了一个空对象。如果我使用 readAsBinaryString,我会看到一堆二进制代码。
var file = document.getElementById('my_file').files[0],
reader = new FileReader();
reader.onloadend = function(e){
console.log(e.target.result);
$scope.image = e.target.result;
}
reader.readAsArrayBuffer(file);
如何查看我的文件,以便我知道它在使用 readAsArrayBuffer 时正在运行? 如果需要更多代码,请告诉我!谢谢。
最佳答案
根据 ArrayBuffer文档
You can not directly manipulate the contents of an ArrayBuffer; instead, you create one of the typed array objects or a DataView object which represents the buffer in a specific format, and use that to read and write the contents of the buffer.
所以正如我之前评论的那样,console.log
可能不知道如何表示缓冲区,因此它只是输出 arrayBuffer:{}
。
如果您想在控制台中显示某些内容,您需要使用类型化数组或数据 View 。例如使用 Int8Array
:
reader.onloadend = function (e) {
console.log(e);
console.log(new Int8Array(e.target.result));
};
参见 demo
关于Javascript - 文件上传,readAsArrayBuffer?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26946548/