skip to Main Content

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:

enter image description here

2

Answers


  1. Chosen as BEST ANSWER

    I just figured out the problem and it was of my own doing unfortunately!!

    I had the following in my ajax request:

    xhrFields: {
        responseType: 'blob' 
    }
    

    but then I introduced credentials support and introduce this

    xhrFields: {
        withCredentials: true
    }
    

    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:

    xhrFields: {
        responseType: 'blob',
        withCredentials: true
    }
    

  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search