Streaming Attachments with PushStreamContent

citas web de issstecali Streaming large files as an attachment can be a pain.  Most of the time you need to have the whole object in memory, on the server, to stream to the client. This can be a massive memory hit on servers and not very performant.

http://skylinemediainc.com/?pokakal=opcje-binarne-literatura&331=2a PushStreamContent was introduced in the Web Client Libraries (and natively as part of .NET 4.5) as a method of pushing content asynchronously to a client.  This can be combined with Web API to stream content efficiently to clients.

http://coleface.com.au/coleface-visit-richglen/?s= Normally a request to serve a large file as an attachment might be as follows:

go to link [HttpGet] public HttpResponseMessage Export(int id) { HttpResponseMessage response = new HttpResponseMessage(); string data = GetAllData(id); response.Content = new StringContent(data); response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); response.StatusCode = HttpStatusCode.OK; response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = "myfile.csv" }; return response; }

click here If GetAllData returns a very large file synchronously, it can lead to blocking of the thread, memory issues on the server, as well as connection timeouts on the client. We can use PushStreamContent to make this more efficient. PushStreamContent allows you to pass a delegate from which you can write to the underlying stream, and that delegate can also be asynchronous. In the example above we can chunk the content to be returned, streaming it to the underlying content stream.

site de rencontre profession medicale [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) { return; } finally { outputStream.Close(); } }, new MediaTypeHeaderValue("application/octet-stream")); response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = "myfile.csv" }; return response; }

follow site This technique can be used with any large file and doesnt need to be an attachment, a similar technique is used here to stream video files. I have used this technique on the azure web explorer project to stream GB size files seamlessly to the client.

Downloading attachments with PushStreamContent

source link  

Leave a Reply

http://flywind.com.br/bakester/6297 Your email address will not be published. Required fields are marked *


go to site