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

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

问题描述

我编写了使用Angular $ http下载文件的代码。该文件的名称是在URL中指定的 not 。 URL包含文件的唯一标识符,该标识符从应用程序外部获取。

I've written code that uses Angular $http to download a file. The name of the file is not specified in the URL. The URL contains a unique identifier for the file, which is fetched from outside the application.

$ http.get(myUrl)被调用,一切正常;检索文件,我可以在我的回调处理程序中访问它,但我看不到如何获取文件的名称。使用Fiddler捕获原始响应,我看到:

When $http.get(myUrl) is called, everything works fine; the file is retrieved and I can access it in my callback handler, but I can't see how to get the name of the file. Capturing the raw response with Fiddler, I see this:

HTTP/1.1 200 OK
Cache-Control: private
Content-Length: 54
Content-Type: application/octet-stream
Server: Microsoft-IIS/8.5
Access-Control-Allow-Origin: http://www.example.com/getFile/12345
Access-Control-Allow-Credentials: true
Content-Disposition: attachment; filename=testfile.txt
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Fri, 09 Oct 2015 20:25:49 GMT

Lorem ipsum dolar sit amet!  The contents of my file!

从上面可以看出,服务器显然正在回复 Content-Disposition,但我还没有找到在我的Angular回调中访问它。如何从标题中获取文件名?

From the above, it is clear that the server is sending back the name of the file in the "Content-Disposition", but I haven't found anyway to access it within my Angular callback. How do I get the name of the file from the headers?

编辑以回应以下答案:
我应该提到在此之前我已经尝试过 response.headers()。一些 Object {content-type:application / octet-stream,cache-control:private} ,所以我仍然没有得到一些Content-Disposition原因。 response.headers('Content-Disposition')返回 null

Edit in response to answer below: I should have mentioned before that I already tried response.headers(). It returns Object {content-type: "application/octet-stream", cache-control: "private"}, so I still don't get Content-Disposition for some reason. response.headers('Content-Disposition') returns null.

推荐答案

值得一提的是,为了从HTTP标头中获取文件名,解压缩 Content-Disposition 标题是不够的。
您仍然需要从此标头值获取文件名属性。

It may be worth mentioning that in order to get the file name from the HTTP headers, extracting the Content-Disposition header is not enough. You still need to obtain the filename property from this header value.

返回的标头值示例:附件; filename =myFileName.pdf

以下函数将提取 filename =myFileName.pdf,然后提取myFileName.pdf并最终移除额外的引号以获得 myFileName.pdf

The function below will extract filename="myFileName.pdf", then extract "myFileName.pdf" and finally remove the extra quotes around to get myFileName.pdf.

您可以使用下面的代码:

You can use the snippet below:

  function getFileNameFromHttpResponse(httpResponse) {
      var contentDispositionHeader = httpResponse.headers('Content-Disposition');
      var result = contentDispositionHeader.split(';')[1].trim().split('=')[1];
      return result.replace(/"/g, '');
  }

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

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