代码之家  ›  专栏  ›  技术社区  ›  user2024080

无法从后端响应获取“Content Disposition”

  •  0
  • user2024080  · 技术社区  · 8 年前

    我想从 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'
        });
      }
    

    我想得到 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
        });
      }
    

    enter image description here

    但根本没有得到价值。

    {
      "headers": {
        "normalizedNames": {
    
        },
        "lazyUpdate": null,
        "lazyInit": null,
        "headers": {
    
        }
      },
      "status": 200,
      "statusText": "OK",
      "url": "https:XXXXXXservice/p/shipment/download/",
      "ok": true,
      "type": 4,
      "body": {
    
      }
    }
    
    1 回复  |  直到 8 年前
        1
  •  0
  •   Jaromanda X    8 年前

    根据 MDN Access-Control-Expose-Headers documentation ,CORS请求只允许javascript访问以下响应头:

    • 内容语言
    • 内容类型
    • 上次修改时间
    • 布拉格

    服务器可以使用 Access-Control-Expose-Headers 回应,所以,为了 Content-Disposition 为了便于客户访问,您需要发布

    Access-Control-Expose-Headers: Content-Disposition
    

    响应中的头

    推荐文章