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.
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!
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?
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
.
response.headers('Content-Disposition') returns null
there's clearly something wrong. The answer from @andrew works perfect – Despondentresponse.headers('Content-Disposition') returns null
and you are using CORS in your API you need to make sure that you add this header in your response:access-control-expose-headers:content-disposition
https://mcmap.net/q/427084/-http-response-headers-missing-in-chrome-but-with-postman-they-show-up – AsarumResponse.Headers.Add(HeaderNames.AccessControlExposeHeaders, "content-disposition");
to the request function and it will give read access to the script for this particular call – Hollingshead