Javascript 以角度将文件转换为uInt8Array
我想使用GolangAPI将文件从angular上传到postgresql 在角度部分,我想将我的Javascript 以角度将文件转换为uInt8Array,javascript,angular,typescript,file-upload,uint8array,Javascript,Angular,Typescript,File Upload,Uint8array,我想使用GolangAPI将文件从angular上传到postgresql 在角度部分,我想将我的文件转换为uInt8Array。我已经转换了数组,但它位于我不知道的内部(如图所示) 那么,如何在变量中获取uInt8Array,比如let x:uInt8Array=y 这是我努力的程度 x.component.html <input (change)="onFileSelected($event)" type="file" id="fil
文件
转换为uInt8Array
。我已经转换了数组,但它位于我不知道的内部(如图所示)
那么,如何在变量中获取uInt8Array,比如let x:uInt8Array=y代码>
这是我努力的程度
x.component.html
<input (change)="onFileSelected($event)" type="file" id="fileUpload">
输出在上一个屏幕截图中。只需从数组缓冲区输出构造一个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");
}
}
代码应该发布在这里。@Pointy如果StackOveerflow有Codepen等工具,就没有理由发布到图像的链接。@Pointy,我的级别低,我也可以这样做吗?现在它成为了一个承诺。如何向Uint8Array作出承诺?(你能更新你的代码让我看一下吗?)我想你在做这样的事情console.log(file.arrayBuffer().then(buff=>newuint8array(buff))代码>然后您将获得承诺输出。但是您应该在promise中执行操作,然后回调。看看promise在JS中是如何工作的。好的,我会查找它,并更新可能会更好的答案。
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");
}
}