如何将字节数组转换为 javascript 中的 blob for angular?
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(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")
我正在以字节数组的形式从后端获取 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(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")