Created
September 12, 2013 02:50
-
-
Save danielmackay/6532580 to your computer and use it in GitHub Desktop.
MVC HttpResponseMessage for returning content as a file. #attachment, #mvc.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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