Upload Files In ASP.NET MVC 5

Creating MVC Application

Let us implement these in a sample Application. Open Visual Studio. Go to File->New->Project. Give a suitable name to the Application. Click OK.



Select MVC Template. Click OK.



Adding Folder

We will add a folder to store the files in the application. Here, I have added a folder in the application.



Adding Controller

Let us add a controller. Right click on the Controller. Add->Controller.



Select MVC 5 Controller -Empty. Click Add.



Give a suitable name to the controller.



Write the following code in the controller.

  1. using System;  
  2. usingSystem.Collections.Generic;  
  3. using System.IO;  
  4. usingSystem.Linq;  
  5. usingSystem.Web;  
  6. usingSystem.Web.Mvc;  
  7. namespaceFileUpload.Controllers  
  8. {  
  9.     public class UploadController: Controller  
  10.     {  
  11.         // GET: Upload  
  12.         publicActionResult Index()  
  13.         {  
  14.             return View();  
  15.         }  
  16.         [HttpGet]  
  17.         publicActionResultUploadFile()  
  18.         {  
  19.             return View();  
  20.         }  
  21.         [HttpPost]  
  22.         publicActionResultUploadFile(HttpPostedFileBase file)  
  23.         {  
  24.             try  
  25.             {  
  26.                 if (file.ContentLength > 0)  
  27.                 {  
  28.                     string _FileName = Path.GetFileName(file.FileName);  
  29.                     string _path = Path.Combine(Server.MapPath("~/UploadedFiles"), _FileName);  
  30.                     file.SaveAs(_path);  
  31.                 }  
  32.                 ViewBag.Message = "File Uploaded Successfully!!";  
  33.                 return View();  
  34.             }  
  35.             catch  
  36.             {  
  37.                 ViewBag.Message = "File upload failed!!";  
  38.                 return View();  
  39.             }  
  40.         }  
  41.     }  
  42. }  
Adding View

Right click on UploadFileActionResult. Go to Add View.



Select the empty template. Click add.



Write the following code in the View.
  1. @{  
  2.     ViewBag.Title = "UploadFile";  
  3. }  
  4.    
  5. <h2>UploadFile</h2>  
  6.    
  7. @using(Html.BeginForm("UploadFile","Upload", FormMethod.Post, new { enctype="multipart/form-data"}))  
  8. {  
  9.       
  10.     <div>  
  11.         @Html.TextBox("file"""new {  type= "file"}) <br />  
  12.    
  13.         <input type="submit" value="Upload" />  
  14.    
  15.         @ViewBag.Message  
  16.    
  17.     </div>  
  18.       
  19.       
  20. }  
Browse the Application

Let us now run the Application and check if it is working fine or not. Browse the Application.



Click upload. I have debugged the code to verify that the file gets uploaded successfully.



The code is working as per the expectations, as it hits the success message. We should get this message on the View, as well.



We will verify the file uploaded, by opening the folder in the Application’s directory.



Summary

Hence, we have just learned how to upload the file in ASP.NET MVC. I hope this post is useful to developers.