Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Jan 30, 2013 01:44 PM by nevo
Member
48 Points
123 Posts
Jun 29, 2012 11:55 AM|LINK
Hello friends,
I want to upload Image into a folder and path of that image into database can anybody
help me to find the solutions.
I am working on Mvc3, Entity Framework Code First
Thanks.
All-Star
38344 Points
6989 Posts
Jun 29, 2012 12:05 PM|LINK
Similar posts:
http://forums.asp.net/t/1679447.aspx/1
http://forums.asp.net/t/1761300.aspx/1
http://haacked.com/archive/2010/07/16/uploading-files-with-aspnetmvc.aspx
Participant
1061 Points
226 Posts
In your model:
@using (Html.BeginForm("ActionName", "ControllerName", FormMethod.Post, new { enctype = "multipart/form-data" ,id ="parentForm"})) { <input type="file" name="file" id="file"/> <input type="submit" value="Create" /> }
When click button create, go to Action in Controller
[HttpPost] public ActionResult Create(Images image,HttpPostedFileBase file) { try { if (file != null) { if (file.ContentLength > 0) { if ((file.ContentType == "image/jpeg") || (file.ContentType == "image/gif") || (file.ContentType == "image/png"))//check allow jpg, gif, png { var fileName = Path.GetFileName(file.FileName); var path = Path.Combine(Server.MapPath("~/Content/ProductImage/"), fileName); file.SaveAs(path);//save image in folder image.Link = file.FileName; _database.Images.Add(image);//add Image object to database (content image path) _database.SaveChanges(); } } } return RedirectToAction("Index"); } catch { return View(); } }
Images is model class content image path
Thanks for reading my post
137649 Points
22143 Posts
Moderator
MVP
Jun 29, 2012 12:07 PM|LINK
http://www.hanselman.com/blog/ABackToBasicsCaseStudyImplementingHTTPFileUploadWithASPNETMVCIncludingTestsAndMocks.aspx
Jun 29, 2012 12:46 PM|LINK
Thank you everyone...
2 Points
1 Post
Jan 30, 2013 01:44 PM|LINK
Hey bkis1112, your example works just fine for me, thx!
kumaru32
Member
48 Points
123 Posts
How to Upload Image in Mvc3
Jun 29, 2012 11:55 AM|LINK
Hello friends,
I want to upload Image into a folder and path of that image into database can anybody
help me to find the solutions.
I am working on Mvc3, Entity Framework Code First
Thanks.
Rajneesh Ver...
All-Star
38344 Points
6989 Posts
Re: How to Upload Image in Mvc3
Jun 29, 2012 12:05 PM|LINK
Similar posts:
http://forums.asp.net/t/1679447.aspx/1
http://forums.asp.net/t/1761300.aspx/1
http://haacked.com/archive/2010/07/16/uploading-files-with-aspnetmvc.aspx
www.rajneeshverma.com
Keep Forums Clean || Use Alert Moderators.
bkis1112
Participant
1061 Points
226 Posts
Re: How to Upload Image in Mvc3
Jun 29, 2012 12:05 PM|LINK
In your model:
@using (Html.BeginForm("ActionName", "ControllerName", FormMethod.Post, new { enctype = "multipart/form-data" ,id ="parentForm"})) { <input type="file" name="file" id="file"/> <input type="submit" value="Create" /> }When click button create, go to Action in Controller
[HttpPost] public ActionResult Create(Images image,HttpPostedFileBase file) { try { if (file != null) { if (file.ContentLength > 0) { if ((file.ContentType == "image/jpeg") || (file.ContentType == "image/gif") || (file.ContentType == "image/png"))//check allow jpg, gif, png { var fileName = Path.GetFileName(file.FileName); var path = Path.Combine(Server.MapPath("~/Content/ProductImage/"), fileName); file.SaveAs(path);//save image in folder image.Link = file.FileName; _database.Images.Add(image);//add Image object to database (content image path) _database.SaveChanges(); } } } return RedirectToAction("Index"); } catch { return View(); } }Images is model class content image path
Thanks for reading my post
Trần Lê Thành Trung
ignatandrei
All-Star
137649 Points
22143 Posts
Moderator
MVP
Re: How to Upload Image in Mvc3
Jun 29, 2012 12:07 PM|LINK
http://www.hanselman.com/blog/ABackToBasicsCaseStudyImplementingHTTPFileUploadWithASPNETMVCIncludingTestsAndMocks.aspx
kumaru32
Member
48 Points
123 Posts
Re: How to Upload Image in Mvc3
Jun 29, 2012 12:46 PM|LINK
Thank you everyone...
nevo
Member
2 Points
1 Post
Re: How to Upload Image in Mvc3
Jan 30, 2013 01:44 PM|LINK
Hey bkis1112, your example works just fine for me, thx!