Uncategorized

Streaming Attachments with PushStreamContent

follow link 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://visitsvartadalen.nu/?saxarokese=K%C3%B6p-Viagra-Arboga&f07=f3 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://www.cilentoescursioni.it/?kiskwa=matematica-e-opzioni-binarie&e54=0c Normally a request to serve a large file as an attachment might be as follows:

dating an asperger woman [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; }

internet dating name and shame 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.

http://nottsbushido.co.uk/hotstore/Hotsale-20150822-310720.html [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; }

100 gratis datingside 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

 

Leave a Reply

enter Your email address will not be published. Required fields are marked *

go

see url

dating other ethnicities