我想使用 golang api 将文件从 angular 上传到 postgresql。
在 Angular 部分,我想将我的文件
转换为uInt8Array
。我已经转换了数组,但它在我不知道的东西里面(如图所示)
screenshot-1
那么我怎样才能在像 let x : uInt8Array = y;
这样的变量中获取 uInt8Array
这是我尝试的程度。
x.component.html
<input (change)="onFileSelected($event)" type="file" id="fileUpload">
x.component.ts
onFileSelected(event) {
console.log("called");
const file: File = event.target.files[0];
if (file) {
console.log(file.arrayBuffer());
console.log("call finished");
}
}
输出在之前的截图中。
只需从数组缓冲区输出构建一个 Uint8Array
file.arrayBuffer().then(buff => {
let x = new Uint8Array(buff); // x is your uInt8Array
// perform all required operations with x here.
});
根据问题应该是这样的
onFileSelected(event) {
console.log("called");
const file: File = event.target.files[0];
if (file) {
file.arrayBuffer().then(buff => {
let x = new Uint8Array(buff); // x is your uInt8Array
// perform all required operations with x here.
console.log(x);
});
console.log("call finished");
}
}