Skip to content

Instantly share code, notes, and snippets.

@belchev
Created December 4, 2015 14:01
Show Gist options
  • Save belchev/6f3d200eed8bbbed9306 to your computer and use it in GitHub Desktop.
Save belchev/6f3d200eed8bbbed9306 to your computer and use it in GitHub Desktop.
public class FileResult : IHttpActionResult
{
private readonly byte[] _fileBinary;
private readonly string _contentType;
private string _fileName;
public FileResult(byte[] fileBinary, string contentType, string fileName)
{
this._fileBinary = fileBinary;
this._contentType = contentType;
this._fileName = fileName;
}
public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
{
var response = new HttpResponseMessage(HttpStatusCode.OK);
//var response = new HttpResponseMessage(HttpStatusCode.OK)
//{
// Content = new StreamContent(File.OpenRead(@"C:\www\templates\test.zip"))
//};
response.Content = new ByteArrayContent(this._fileBinary);
response.Content.Headers.ContentType = new MediaTypeHeaderValue(this._contentType);
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
response.Content.Headers.ContentDisposition.FileName = this._fileName;
return Task.FromResult(response);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment