Skip to content

Instantly share code, notes, and snippets.

@danielmackay
Created September 12, 2013 02:50
Show Gist options
  • Save danielmackay/6532580 to your computer and use it in GitHub Desktop.
Save danielmackay/6532580 to your computer and use it in GitHub Desktop.
MVC HttpResponseMessage for returning content as a file. #attachment, #mvc.
public class FileDownloadHttpResponseMessage : HttpResponseMessage
{
public FileDownloadHttpResponseMessage(string fileName, string content)
: base(HttpStatusCode.OK)
{
Content = new StringContent(content);
// A text file is actually an octet-stream (pdf, etc)
Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
// We used attachment to force download
Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
Content.Headers.ContentDisposition.FileName = fileName;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment