Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save david-mills/7ab14cb0f8ff714db687 to your computer and use it in GitHub Desktop.
Save david-mills/7ab14cb0f8ff714db687 to your computer and use it in GitHub Desktop.
[HttpGet]
public HttpResponseMessage Export(int id)
{
var response = Request.CreateResponse();
response.Content = new PushStreamContent(
async (outputStream, httpContent, transportContext) =>
{
try
{
for (int page = 0; page < 10; page++)
{
string data = await GetData(id, page);
var buffer = Encoding.UTF8.GetBytes(data);
await outputStream.WriteAsync(buffer, 0, buffer.Length);
}
}
catch (HttpException ex)
{
if (ex.ErrorCode == -2147023667) // The remote host closed the connection.
{
return;
}
}
finally
{
outputStream.Close();
}
}, new MediaTypeHeaderValue("application/octet-stream"));
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = "myfile.csv"
};
return response;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment