Skip to content

Instantly share code, notes, and snippets.

@benerdin
Last active August 29, 2015 14:18
Show Gist options
  • Select an option

  • Save benerdin/d0e2c2836c0333a6bd9f to your computer and use it in GitHub Desktop.

Select an option

Save benerdin/d0e2c2836c0333a6bd9f to your computer and use it in GitHub Desktop.
ASP.NET Web API - Download File
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;
namespace Web.Something
{
public class SomeController
{
[HttpGet]
public Task<HttpResponseMessage> Export(Guid id)
{
// Build the file name.
var fileDownloadName = "TODO: Build file name";
// Build the file contents.
var fileContents = "TODO: Build file contents here";
// Set the headers to indicate we are returning a file.
var downloadContent = new StringContent(fileContents);
downloadContent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
downloadContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = fileDownloadName
};
// Respond with the file.
return new HttpResponseMessage(HttpStatusCode.OK)
{
Content = downloadContent
};
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment