I’ve a got a function in my web api 2.0 to download a file but hadn’t tried it in a while and only discovered yesterday that it was no longer working. I’ve partially fixed the issue with createObjectURL
but one thing I’ve noticed is that while the Content-Disposition
is set in my web api:
public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
{
var response = new HttpResponseMessage();
var filename = this.Document.GetFilename();
var mimeType = MimeMapping.GetMimeMapping(filename);
response.Content = new StreamContent(new MemoryStream(this.Document.ToData()));
response.Content.Headers.ContentLength = this.Document.Data.Length;
response.Content.Headers.ContentType = new MediaTypeHeaderValue(mimeType);
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = filename
};
return Task.FromResult(response);
}
Yet when I check it in JavaScript
, it always null from the response header:
success: function (blob, status, xhr) {
var filename = "";
var disposition = xhr.getResponseHeader('Content-Disposition');
...
}
Any ideas why?
Thanks.
UPDATE-1:
The content disposition appears to be returned when I check the response in the Network
section of the browser but when I call xhr.getAllResponseHeaders()
or xhr.getResponseHeader('Content-Disposition');
, it is not returned by either function calls as you can see in the snapshot below:
2
Answers
I just figured out the problem and it was of my own doing unfortunately!!
I had the following in my ajax request:
but then I introduced credentials support and introduce this
but I didn't spot this until this morning when I realized I stupidly declared as above clearly i.e. 2 statements, thus, overwriting the
responseType: 'blob'
setting when I should have declared it as:From server side, please set this header in response
"Access-Control-Expose-Headers”
with value“*”
to make all headers in response to be read by frontend script. Or just set“Content-Disposition”
for this case, instead of *, to only allow reading value of content-disposition header by frontend script.