nizam nizam

nizam nizam

  • NA
  • 0
  • 12.3k

MVC 4 How to upload image file and show

Feb 20 2015 7:19 AM
namespace StudentApp.Models
{
[MetadataType(typeof (Sdetails))]
public partial class tbl_Sdetails
{
[Display(Name = "Photo")]
[FileTypes("jpeg,png,jpg",ErrorMessage = "Invalid format")]
public HttpPostedFileWrapper PostedPhoto { get; set; }
public string PhotoPath { get; set; }
}
 
<h2>SdetailsAdd</h2>
@using (Html.BeginForm("SdetailsAdd","Sdetails", FormMethod.Post,new {enctype="multipart/form-data"}))
{
 
<td>@Html.LabelFor(a => a.Image)</td>
@Html.TextBoxFor(m=>m.PostedPhoto,string.Empty,new {@type="file"})
 
public ActionResult SdList(string txtSearch)
{
var sd = db.tbl_Sdetails.ToList();
if (!string.IsNullOrEmpty(txtSearch))
{
sd = sd.Where(s => s.studentName.ToUpper().Contains(txtSearch.ToUpper())).ToList();
}
foreach (var student in sd)
{
if (student.Image != null)
{
string.Format("data; image/gif;based64,{0}", Convert.ToBase64String(student.Image));
}
}
return View(sd);
}
 
 

Answers (4)