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

gowsalya said…
Really nice experience you have. Thank you for sharing. It will surely be an experience to someone.
python course in pune
python course in chennai
python Training in Bangalore
deiva said…
Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.
hadoop training in chennai

hadoop training in omr

salesforce training in chennai

salesforce training in omr

c and c plus plus course in chennai

c and c plus plus course in omr

machine learning training in chennai

machine learning training in omr



Popular posts from this blog

The specified initialization vector (IV) does not match the block size for this algorithm