我正在以位元組陣列的形式從后端獲取 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")
});
uj5u.com熱心網友回復:
如果您正確獲得了位元組陣列,則此函式可能有助于正確獲取型別
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")
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/364359.html
標籤:javascript 有角的 打字稿 实体框架
