Webapi file download






















After successfully learning how to upload files, the logical next step is to learn how to download them as well. As in the previous article , for downloading purposes, we are going to work with pictures. Feel free to apply this implementation logic to other types of files.

In this part, since we are adding new functionality, we will first adjust the current implementation. The adjustments will cover the addition of a service that contains all the desired logic for handling files in general. That way, we are improving readability and opening opportunities for extending the service in the future with more file handling operations.

Currently, we have an upload button in our form for creating a user. When we create a user, the uploaded picture becomes the profile picture. For the sake of simplicity, we will reuse the current implementation and extend it to implement file download.

That said, the pictures that we upload during user creation will be the files that we are going to download after. That way we are wrapping up the file handling logic into one controller that contains endpoints for every operation. Given that we are focusing on a file download, the next step is to extend the controller with a new endpoint for download operation.

At the moment, the method will only return a successful result with a corresponding message:. This step was not necessary, but it will help us in knowing which end-point we actually want to execute from the client-side.

For the time being, we will leave the download operation to return only a successful result and, in the meantime, start with the implementation of the client-side. That way, we can have a clearer view of what are the potential needs of a user and how to modify the API to fulfill those needs. Changing the name to UploadController and adding Route, its end-point has changed as well, therefore we have to update the code in the upload.

Now try to run the application again and confirm that it is working as if nothing has changed! So do not feel disheartened. I was just pointing out some resources you can use to support your answer. Yes this is true. Here is a simplistic example of streaming a file: using System. IO; using Microsoft. GetFileName model. ReadAllBytesAsync model. Tanvir Tanvir 21 1 1 bronze badge. Following is the basic example of returning file e. ContentType, result. Adeel Ahmed Adeel Ahmed 31 5 5 bronze badges.

FileStreamResult works for me. I don't know how it can work. Sign up or log in Sign up using Google. Sign up using Facebook. Below are a few examples of MIME — content types for your information. Please bookmark this page and share this article with your friends and Subscribe to the blog to get a notification on freshly published best practices of software development. This article was helpful.

I did write my custom interface in the above code which returns the stream and achieved the result. Hello James , Thanks for your query. I shall soon out the code on my GitHub. ReadAsStream id ;.

Hello Ben, Thanks for your query. Part boundaries are indicated by the lines that start with dashes. The part boundary includes a random component "" to ensure that the boundary string does not accidentally appear inside a message part.

In the previous example, the user uploaded a file named GrandCanyon. The controller will read the files asynchronously. Web API supports asynchronous actions using the task-based programming model. First, here is the code if you are targeting.

NET Framework 4. Show 5 more comments. FromBase64String file. OK ; result. ContentDispositionHeaderValue "attachment" ; result. This actually answers the question — Mick. This would not be a good idea with large files since it loads the entire image into memory. The stream option is better. PaulReedy Perfect But I totally agree with your point!

Amir Shirazi Amir Shirazi 7 7 silver badges 9 9 bronze badges. This works great, Also if you wanna control the file name there is a property on FileContentResult called FileDownloadName to specify the filename — weeksdev. Thanks for the comment. Thats it, thanks. Also comment from weeksdev is very useful. OK ; responseMsg.

SomeData ; responseMsg. ContentDispositionHeaderValue "attachment" ; responseMsg. FileName; responseMsg.



0コメント

  • 1000 / 1000