Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to upload image and display immediately in box provided. I am saving path to database. So again on another page i want to display that image.

code in view

XML
 <div style="float:left; margin-left:100px">
Upload Image
 <input type="file" name="image_icon" id="img" />

 </div>


code in controller

C#
public ActionResult Create(FormCollection collection, sampleViewModel sampleVM, HttpPostedFileBase image)
{
 try
 {
 //if (image != null)
 if(sampleVM.image_icon!=null)
 {
     // var imageName = image.FileName;
     var imageName = newsVM.image_icon;
     var filePathOriginal = Server.MapPath("/Content/Image");
     //var filePathThumbnail = Server.MapPath("/Content/Uploads/Thumbnails");
     string savedFileName = Path.Combine(filePathOriginal, imageName);
     image.SaveAs(savedFileName);
 }
 }
}


But i am getting image as null. And also want display image when i browse image from filedialogue.
Posted
Updated 4-Aug-15 23:16pm
v2
Comments
F-ES Sitecore 4-Aug-15 8:08am    
What have you tried so far? Have you googled "upload and show image asp.net mvc"? Or "store image path in database asp.net mvc"? Do you have a specific problem or question?
Mukesh Pr@sad 4-Aug-15 8:20am    
pls post ur code...and specify that where u got stucked..!
Member 9579525 4-Aug-15 9:29am    
//code in view
<div style="float:left; margin-left:100px">
<lable>Upload Image
<input type="file" name="image_icon" id="img" />

</div>

//code in controller
public ActionResult Create(FormCollection collection, sampleViewModel sampleVM, HttpPostedFileBase image)
{
try
{

//if (image != null)
if(sampleVM.image_icon!=null)
{
// var imageName = image.FileName;
var imageName = newsVM.image_icon;
var filePathOriginal = Server.MapPath("/Content/Image");
//var filePathThumbnail = Server.MapPath("/Content/Uploads/Thumbnails");
string savedFileName = Path.Combine(filePathOriginal, imageName);

image.SaveAs(savedFileName);
}
}
}
But i am getting image as null. And also want display image when i browse image from filedialogue.
F-ES Sitecore 5-Aug-15 5:17am    
The action param is called "image" but your input name is "image_icon". The two have to match.
Member 9579525 5-Aug-15 6:26am    
It works..thank you.

1 solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900