[英]How can i get file name inside “content-disposition” from a Response headers?
標頭在內容處置中發送filename =“ name.xlsx”,但如何將其設置為我正在下載的文件?
component.ts:
public getAccordo(event: any): any {
event.preventDefault();
this.accordiService.getAccordo(this.filter).subscribe(
data => {
const blob = new Blob([data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
const anchor = document.createElement('a');
anchor.href = window.URL.createObjectURL(blob);
anchor.download = name
anchor.click();
},
error => {
console.log(error);
}
);
}
Service.ts
getAccordo(filtro:篩選器){
return this.http.post<any>(
`${this.ENDPOINT}/export`,
filter,
{responseType : 'blob' as 'json'}
);}
要從響應中獲取headers
,您需要在HttpOptions
設置{observe: 'response'}
參數。
this.http.post<any>(`${this.ENDPOINT}/export`, filter, {
responseType : 'blob' as 'json',
observe: 'response'
})
然后在subscribe
函數中,您可以通過headers
屬性獲取標題。 獲取名稱后,您現在可以設置文件的名稱。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.