Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post May 02, 2012 05:46 AM by Young Yang - MSFT
Member
2 Points
5 Posts
Apr 30, 2012 12:03 PM|LINK
how to upload image in .cshtml file?? am new
Contributor
2066 Points
416 Posts
Apr 30, 2012 12:13 PM|LINK
This tutorial can help
http://weblogs.asp.net/jalpeshpvadgama/archive/2012/04/20/file-upload-in-asp-net-mvc3.aspx
Regards
All-Star
21111 Points
1817 Posts
Microsoft
May 02, 2012 05:46 AM|LINK
Hi
You can use HttpPostedFileBase.InputStream.Read() method read the upload stream to a buffer, and then save it to the database field. Like this:
[HttpPost] public ActionResult Create(string fileTitle) { try { HttpPostedFileBase file = Request.Files[0]; byte[] imageSize = new byte[file.ContentLength]; file.InputStream.Read(imageSize, 0, (int)file.ContentLength); Image image = new Image() { Name = file.FileName.Split('\\').Last(), Size = file.ContentLength, Title = fileTitle, ID = 1, Image1 = imageSize }; db.Images.AddObject(image); db.SaveChanges(); return RedirectToAction("Detail"); } catch(Exception e) { ModelState.AddModelError("uploadError", e); } return View(); }
In View:
@using (Html.BeginForm("Create", "Home", FormMethod.Post, new { enctype = "multipart/form-data" })) { <fieldset> <legend>Upload Image</legend> @Html.Label("Title") @Html.Editor("fileTitle")<br /> Upload File: <input type="file" name="test" /> <p> <input type="submit" value="Create" /> </p> </fieldset> }
Hope this helpful Regards Young Yang
fuzzy_boy001
Member
2 Points
5 Posts
how to upload image in .cshtml file?
Apr 30, 2012 12:03 PM|LINK
how to upload image in .cshtml file?? am new
Gaspard
Contributor
2066 Points
416 Posts
Re: how to upload image in .cshtml file?
Apr 30, 2012 12:13 PM|LINK
This tutorial can help
http://weblogs.asp.net/jalpeshpvadgama/archive/2012/04/20/file-upload-in-asp-net-mvc3.aspx
Regards
Young Yang -...
All-Star
21111 Points
1817 Posts
Microsoft
Re: how to upload image in .cshtml file?
May 02, 2012 05:46 AM|LINK
Hi
You can use HttpPostedFileBase.InputStream.Read() method read the upload stream to a buffer, and then save it to the database field. Like this:
[HttpPost] public ActionResult Create(string fileTitle) { try { HttpPostedFileBase file = Request.Files[0]; byte[] imageSize = new byte[file.ContentLength]; file.InputStream.Read(imageSize, 0, (int)file.ContentLength); Image image = new Image() { Name = file.FileName.Split('\\').Last(), Size = file.ContentLength, Title = fileTitle, ID = 1, Image1 = imageSize }; db.Images.AddObject(image); db.SaveChanges(); return RedirectToAction("Detail"); } catch(Exception e) { ModelState.AddModelError("uploadError", e); } return View(); }In View:
@using (Html.BeginForm("Create", "Home", FormMethod.Post, new { enctype = "multipart/form-data" })) { <fieldset> <legend>Upload Image</legend> @Html.Label("Title") @Html.Editor("fileTitle")<br /> Upload File: <input type="file" name="test" /> <p> <input type="submit" value="Create" /> </p> </fieldset> }Hope this helpful
Regards
Young Yang
Feedback to us
Develop and promote your apps in Windows Store