File upload example in asp.net mvc and razor

File upload example in asp.net mvc and razor

In this tutorial i am going to explain about File upload example in asp.net mvc and razor. In my previous tutorial i have explained about

Implementation :

Now we will see the implementation of File upload example in asp.net mvc and razor.  Below i have explained it step by step.

  1. Create a new project and name it as UploadFile.
  2. Add a new controller in controllers folder and name it as UploadController.cs
  3. Add two action methods with same as UploadFile – one with Get method and another one with Post method.
  4. Get method won’t accept any parameters and Post method will accept HttpPostedFileBase as input.
  5. Now add the code to save the uploaded file in the post method.

Below is the complete code of UploadController.cs

Now right click on UploadFile action method and select option Add View.  And add the below code in the view.

The view contains file upload control and a button enclosed with in a form element. Here Html.BeginForm accepts four parameteres in the order Action Name,Controller Name, Form Method and additional  attributes. Since we are uploading file we need to specify the enctype as multipart/form-data.

Output:

Now if you run the program you can see the below output for File upload example in asp.net mvc and razor.

Output of File upload example in asp.net mvc and razor

Conclusion:

Hope you learned File upload example in asp.net mvc and razor. Do you like this tutorial? Help us to improve. Please post your comments and feedback below.

Leave a Reply

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