Created
June 9, 2014 17:44
-
-
Save david-mills/7ab14cb0f8ff714db687 to your computer and use it in GitHub Desktop.
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
[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