簡體   English   中英

如何從Response標頭中的“ content-disposition”中獲取文件名?

[英]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.

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