簡體   English   中英

如何將 javascript 中的字節數組轉換為 angular 中的 blob?

[英]How to convert byte array to blob in javascript for angular?

我從后端以字節數組的形式獲取 excel 文件。 我想將該字節數組轉換為 blob,然后轉換為文件類型。 請看一下我使用過的以下代碼。

this.getFile().subscribe((response)=>{
const.byteArray=new Uint8Array(atob(response.data).split('').map(char)=>char.charCodeAt(0))
this.pdfResult=new(Blob[byteArray],{type:"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"});
let file=new File([this.pdfResult],"sample.xlsx")
});

如果你有正確的字節數組,這個 function 可能有助於正確輸入

  function(ext) {
        if (ext != undefined) {
            return this.extToMimes(ext);
        }
        return undefined;
    }

extToMimes(ext) {
        let type = undefined;
        switch (ext) {
            case 'jpg':
            case 'png':
            case 'jpeg':
                type = 'image/jpeg'
                break;
            case 'txt':
                type = 'text/plain'
                break;
            case 'xls':
                type = 'application/vnd.ms-excel'
                break;
            case 'doc':
                type = 'application/msword'
                break;
            case 'xlsx':
                type = 'application/vnd.ms-excel'
                break;
            default:

        }
        return type;
    }


let _type = this.function(fileExtension.toLowerCase());
const blob = new Blob([byteArray], { type: _type });
let file=new File([this.pdfResult],"sample.xlsx")



                          

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM