How To Upload Image And Save Image In Project Folder In MVC

Today i am explaining how to upload image in mvc and save the image in project folder.
 
Create a new MVC project, and choose the following template.
 
 
 
Here is my solution explorer of the project.

 

Right click on the project and follow the following steps for adding Entity data model to it.
 
 

Click on add New Item-Data->ADO.NET Entity Data Model

 


Click-Next

Choose your connection string  and choose the 'Yes'  rodio button so that it will automatically update in your web.config.
 
 
 
Now specify the tables you want.
 
When you click finish it will add your table to the ORM designer as follow.Here your table is mapped as a class and all the fields inside the table act as property.
 
 
 
When you click finish it will add your table to the ORM designer pattern.here your table is mapped as a class and the colums as property.
 
 
 
So here we have 3 fields.
 
Now we are comming to our main requirement.
 
Add a img folder to the project so that our images can store here.
 
 
Go to the home controller and  write the following code here i have described each line of code
  1. public class HomeController: Controller   
  2. {  
  3.     MVCEntities obj = new MVCEntities();  
  4.     public ActionResult Index() {  
  5.         return View();  
  6.     }  
  7.     [HttpPost]  
  8.     public ActionResult Index(FormCollection fc, HttpPostedFileBase file)   
  9.     {  
  10.         tbl_details tbl = new tbl_details();  
  11.         var allowedExtensions = new[] {  
  12.             ".Jpg"".png"".jpg""jpeg"  
  13.         };  
  14.         tbl.Id = fc["Id"].ToString();  
  15.         tbl.Image_url = file.ToString(); //getting complete url  
  16.         tbl.Name = fc["Name"].ToString();  
  17.         var fileName = Path.GetFileName(file.FileName); //getting only file name(ex-ganesh.jpg)  
  18.         var ext = Path.GetExtension(file.FileName); //getting the extension(ex-.jpg)  
  19.         if (allowedExtensions.Contains(ext)) //check what type of extension  
  20.         {  
  21.             string name = Path.GetFileNameWithoutExtension(fileName); //getting file name without extension  
  22.             string myfile = name + "_" + tbl.Id + ext; //appending the name with id  
  23.             // store the file inside ~/project folder(Img)  
  24.             var path = Path.Combine(Server.MapPath("~/Img"), myfile);  
  25.             tbl.Image_url = path;  
  26.             obj.tbl_details.Add(tbl);  
  27.             obj.SaveChanges();  
  28.             file.SaveAs(path);  
  29.         } else {  
  30.             ViewBag.message = "Please choose only Image file";  
  31.         }  
  32.         return View();  
  33.     }  
  34. }  
Right click on Index action method and create a view like this.
 
 
 
The source code for the view is- 
  1. @using(Html.BeginForm("Index""Home", FormMethod.Post, new {  
  2.     enctype = "multipart/form-data"  
  3. })) { < div > < img id = "user_img"  
  4.     height = "100"  
  5.     width = "90"  
  6.     style = "border:solid" / > < /div>  
  7. <div>  
  8. <input type="file" title="search image" id="file" name="file" onchange="show(this)" / > < /div>  
  9. <div>  
  10. UserId  
  11. </div > < div > < input type = "text"  
  12.     id = "txt_id"  
  13.     name = "id" / > < /div>  
  14. <div>  
  15. UserName  
  16. </div > < div > < input type = "text"  
  17.     id = "txt_name"  
  18.     name = "Name" / > < /div>  
  19. <div>  
  20. <input type="submit" title="save" / > < /div>  
  21. }  
Now i want when i choose the image it should be dispalayed in the image control for this this is my Javascript.
  1. <script src="~/Scripts/jquery-1.7.1.min.js"></script>  
  2. <script type="text/javascript">  
  3.    function show(input) {  
  4.       if (input.files && input.files[0]) {  
  5.       var filerdr = new FileReader();  
  6.       filerdr.onload = function (e) {  
  7.          $('#user_img').attr('src', e.target.result);  
  8.       }  
  9.       filerdr.readAsDataURL(input.files[0]);  
  10.    }  
  11. }  
  12. </script>  
Add this script just above the form tag.
 
Now run the project.
 
As it open start add  any information.
 
 
 
Thus this will save our image in img folder and url in database.
 
And one more things if we are taking other extensions it will not save in database.
 
In this way we can perform our operation.
 
Thanks you. 
Ebook Download
View all
Learn
View all