Skip to content

Instantly share code, notes, and snippets.

@shawnmclean
Created April 6, 2012 21:16
Show Gist options
  • Save shawnmclean/2322994 to your computer and use it in GitHub Desktop.
Save shawnmclean/2322994 to your computer and use it in GitHub Desktop.
Return a file content from asp.net webapi action
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