How to set downloading file name in ASP.NET Web API
You need to set the Content-Disposition
header on the HttpResponseMessage
:
HttpResponseMessage response = new HttpResponseMessage();
response.StatusCode = HttpStatusCode.OK;
response.Content = new StreamContent(result);
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = "foo.txt"
};
EDIT:
As mentioned in a comment, My answer doesn't account for characters that need to be escaped like a ;
. You should use the accepted answer Darin made if your file name could contain a semi-colon.
Add a Response.AddHeader to set the file name
Response.AddHeader("Content-Disposition", "attachment; filename=*FILE_NAME*");
Just change FILE_NAME to the name of the file.