retrieve file async over webapi in .dotnet core



WebApi in .Netcore makes it really easy to  retrieve / download file asynchronously.

There are 2 different ways to go about this (could be more) but here's my approach

a) ActionResult

b) FileStreamResult

c) CustomActionResult


Check out the following codes (a and b - ain't that much of a difference)




FileStreamResult is async operation. You will get read chunk by chunk, asynchronously and delivered to client.

What about ActionResult?


Tricky part would be (c) creating your custom ActionResult to read and send to client. A huge differences to note is that, traditionally we use HttpMessageResponse. But this introduce conversion challenges and with dotnet core, we gonna have a huge incompatibilities issues too.

But WebAPI approach to solve this is pretty easy. Take a look at code below :-



As you can see, instead of creating you HttpMessageResponse manually, you can just leverage on existing ones. :)


Comments

Sathya G said…
Really its a amazing article that I had ever read. I hope it will help a lot for all. Thank you so much for this amazing posts and please keep update like this excellent article. Thank you for sharing such a great blog with us. Expecting for your update.
Dotnet Training in Chennai

Popular posts from this blog

OpenCover code coverage for .Net Core

Android Programmatically apply style to your view

Using Custom DLL with IronPython / Scripts