X-Requested-With: XMLHttpRequest
No set of HTTP headers is going to cause the browser to download a file it gets in response to a request using XHR.
You have three basic options.
- Don't use XHR in the first place
- Store the file data somewhere, give it a temporary URI, pass the URI back in the response, have the client side JS set
location
to that URI - Construct a
data:
scheme URI and have the client side JS assign it tolocation
.
Unless you really need to sometimes return a file, and sometimes return data for JS to process (e.g. error messages) then option 1 is the best.