如何获取使用Angular5下载的文件的名称 [英] How to get the name of a file downloaded with Angular5

查看:71
本文介绍了如何获取使用Angular5下载的文件的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的回复标题为:

HTTP/1.1 200 OK
Content-Disposition: attachment; filename="file.docx"
Accept-Ranges: bytes
Cache-Control: public, max-age=0
Last-Modified: Thu, 26 Apr 2018 10:37:00 GMT
ETag: W/"c61-16301871843"
Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document
Content-Length: 3169
Date: Thu, 26 Apr 2018 10:37:00 GMT
Connection: keep-alive

我尝试了这段代码,

 public download(id: string): Observable<Blob> {
    let headers = new Headers();
    const _baseUrl = (Api.getUrl(Api.URLS.download));
    headers.append('x-access-token', this.auth.getCurrentUser().token);
    headers.append('sale_id', id);
    return this.http.get(_baseUrl, { headers: headers, responseType: ResponseContentType.Blob})
      .map((res) => {
        console.log(res.headers) // show like in image
        return new Blob([res.blob()], { type: 'application/octet-stream' })
      });
  }

在控制台中显示的

:

that show in console:

没有显示内容配置!!

Didn't show Content-Disposition !!

如何从标题中获取文件名?

How do I get the name of the file from the headers?

推荐答案

您的后端需要返回特定的标头,

Your backend needs to return a specific header, Access-Control-Expose-Headers. If you don't, Angular doesn't expose the header, explaining why you can't see it.

您可以在此处找到更多信息(尽管有点旧版本)和此处还提供了更多信息

You can find more information here (although it's a bit old) and even more information here

这篇关于如何获取使用Angular5下载的文件的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆