Created
April 6, 2012 21:16
-
-
Save shawnmclean/2322994 to your computer and use it in GitHub Desktop.
Return a file content from asp.net webapi action
This file contains 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 HttpResponseMessage GetText() | |
{ | |
try | |
{ | |
string content = "Hello"; | |
HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK); | |
result.Content = new StringContent(content); | |
//a text file is actually an octet-stream (pdf, etc) | |
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); | |
//we used attachment to force download | |
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment"); | |
result.Content.Headers.ContentDisposition.FileName = "mytext.txt"; | |
return result; | |
} | |
catch (Exception ex) | |
{ | |
throw new HttpResponseException(HttpStatusCode.InternalServerError); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment