This article will guide you how to upload a file in the database and download it from the database(SQL Server) using ASP.Net Web API and Entity Framework. To consume Web API you can simply use JQuery Ajax.
Create a Table in the Database
First, you need a table where file related data will be stored. Let’s create a table name uploaded_files in a database. You can have your own database name as well as table name as per your project requirement.
Creating WEB API
In order to create a WEB API, you can follow the given steps
- Right click on Controllers folder from solution explorer
- Click on Add
- Click on Controller
- Select the Web API 2 Controller – Empty option from Add Scaffold Window
- Click on Add button
- Give a name for Controller, example FileAPIController
Once you create FileAPIController inside Controllers folder, you have to register the Configuration for Web API in the Global.asax file so that the Web API is available for accessing on Web. To register WebApiConfig you have to use given code in Global.asax.
// GlobalConfiguration class reside in System.Web.Http Namespace.
You can see in below code how to register WebApiConfig in global.asax.cs file
Read more at – https://docs.microsoft.com/en-us/aspnet/web-api/overview/advanced/configuring-aspnet-web-api about how to configure Web API.
If you are novice learner in ASP.Net WEB API, I would like to request you please have a look at code-adda another article for beginner. Creating your first WEB API Project
Write Http Actions in Web API
Once you create API Controller you have to write code to perform tasks. Here I have created FileAPIController to upload a file in the database and retrieve the file from the database. I am going to write three actions for various purposes like user can upload a file and save in database, the user can read all files from database and download any specific file as per their need.
Different actions are –
- UploadFile | Type – HttpPost
- GetFiles | Type – HttpPost
- GetFile | Type – HttpGet
Please go through below code and understand line by line. If you have a little bit knowledge of Asp.net WEB API, you can understand the code easily.
UploadFile – This action is a HttpPost method which will allow the user to save the file in the database. file data getting converted to Byte Array which will be stored in the database along with filename and content type.
GetFile – This action is a HttpGet method which will allow the user to retrieve a file from the database into the page. Read ByteArrayContent and assign to http_Response.Content and Set the ContentDisposition Header Value and FileName. Use depositionType as attachment in ContentDispositionHeaderValue
Test API Using Postman
It is always good to test your API you have written before you jump directly into writing code. You can use either Fiddler or Postman or some other tools to test your API. Here I am using Postman. Please go through with steps mention in below image and you’ll get desired output.
Once you have tested your API in API testing tool, you can check saved data in the table. Have a look at below picture.
Consume WEB API
I am assuming you must have Idea about how to add Controller and View in ASP.Net MVC project. If not, please go through official Microsoft docs to read how to Create application in MVC.
Build the code and run the application, Navigate that particular page in the browser where you’ve written code to consume API and then upload files after that you’ll get the output as below image.