簡體   English   中英

無法從后端響應獲取“ Content-Disposition”

[英]Not able to get the `Content-Disposition` from backend response

我正在嘗試從后端響應中從Content-Disposition獲取值。 但我根本無法理解。

這是我的代碼:

public getQuoteImage(sharedQuote):Observable<any> {
    return this.http.post(environment.downloadSummarUrl, JSON.stringify(sharedQuote), { 
      headers: this.params.headers.validate,
      observe: "response",
      responseType : 'blob'
    });
  }

我正在嘗試從以下Content-DispositionContent-Disposition獲取filename

downloadSummaryContent() {
    this.server.getQuoteImage(this.sharedQuote).subscribe((data) => {
      console.log( 'headers are', '\n', data, '\n',  data.headers.get('Content-Disposition') ); //but not getting the file name
      var url = window.URL.createObjectURL(data);
      var a = document.createElement('a');
      document.body.appendChild(a);
      a.setAttribute('style', 'display: none');
      a.href = url;
      a.download = 'quote.pdf';
      a.click();
      window.URL.revokeObjectURL(url);
      a.remove(); // remove the element
    });
  }

在此處輸入圖片說明

但根本無法獲得價值。

在控制台中,我得到:

{
  "headers": {
    "normalizedNames": {

    },
    "lazyUpdate": null,
    "lazyInit": null,
    "headers": {

    }
  },
  "status": 200,
  "statusText": "OK",
  "url": "https:XXXXXXservice/p/shipment/download/",
  "ok": true,
  "type": 4,
  "body": {

  }
}

根據MDN Access-Control-Expose-Headers文檔 ,CORS請求僅允許javascript訪問以下響應頭:

  • 緩存控制
  • 內容語言
  • 內容類型
  • 過期
  • 上一次更改
  • 語用

服務器可以使用Access-Control-Expose-Headers響應允許訪問其他響應頭,因此,要讓客戶端可以訪問Content-Disposition ,您需要發出

Access-Control-Expose-Headers: Content-Disposition

響應中的標題

暫無
暫無

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

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