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; }
}
SdetailsAdd
@using (Html.BeginForm("SdetailsAdd","Sdetails", FormMethod.Post,new {enctype="multipart/form-data"}))
{
@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);
}
Manish Kumar ChoudharyPosted Feb 20, 2015, 7:39 AM
Create a Method Like
private string UploadEmployeeImage(HttpPostedFileBase file)
{
string fileName = string.Empty;
fileName = string.Format(@"{0}.{1}", Guid.NewGuid(), Path.GetExtension(file.FileName));
// var path = Server.MapPath("~/App_Data/Files");
var path = Server.MapPath("~/EmployeeImages");
string savedFileName = Path.Combine(path, Path.GetFileName(fileName));
file.SaveAs(savedFileName);
return fileName;
}
Call this method inside your controller action method like.
if (Request.Files["ImageFileToUpload"].ContentLength >0)
{
var file = Request.Files["ImageFileToUpload"];
///Saving the file to EmployeeImages folder with unique name.
fileName = UploadEmployeeImage(file);
}
Inside View use Following code
@using (Html.BeginForm("Edit", "Employees", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<div class="col-lg-6">
<input type="file" id="fileToUpload" name="ImageFileToUpload" class="form-control" />
div>
}
Posted Feb 20, 2015, 7:32 AM
Posted Feb 20, 2015, 7:30 AM
Suraj SahooPosted Feb 20, 2015, 7:26 AM
I am procing you with few good links which will definitely help you in this query:-
http://stackoverflow.com/questions/16255882/uploading-displaying-images-in-mvc-4
http://www.c-sharpcorner.com/UploadFile/b696c4/how-to-upload-and-display-image-in-mvc/
The second one is a very good article.
Please go through.
I hope these help.
Accept if helps anyway.
Thanks