javascript - 以 Angular 将文件转换为 uInt8Array

标签 javascript angular typescript file-upload uint8array

<分区>

我想使用 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");
     }
}

关于javascript - 以 Angular 将文件转换为 uInt8Array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66968589/

相关文章:

javascript - 如何在 typescript 和 angular 中使用 $scope.$on?

javascript - 与 Logo 图像位于同一顶部导航栏上的汉堡包菜单

html - 登录时隐藏可选的导航栏菜单 - Angular 8

html - CSS Only sidenav 不会在路由时关闭

reactjs - 错误 TS2322 : Property 'css' does not exist on type 'DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>'

javascript - 将参数传递给 oneeachfeature 传单

javascript - 包裹在 'select' 标签内时的 HTML 和 CSS 可能性

javascript - 浏览器控制台输出在带有 settimeout 的 for 循环中有一个神秘的数字?

javascript - 对象中的 "store"对象好不好

angular - Dotnet Angular 模板和 Core 2.0 MVC