0

I try to upload an image using MVC4 so i create a view that holds an input file to select file by user :

<div class="editor-label"> @Html.LabelFor(model => model.ImageUrl) </div> <input id="ImageUrl" title="Upload a student image" type="file" name="ImageUrl" /> 

The ImageUrl holds the name of file ,after submit i send this form to my action:

[HttpPost] [Authorize(Roles = "Admin")] public ActionResult Create(Student student) { student.RegisterDate = DateTime.Now.Date; if (objcheck.CheckUserExistAlready(student.Email)) { string strLocation = HttpContext.Server.MapPath("~/App_Data"); Request.SaveAs(strLocation + @"\" + student.ImageUrl,true); obj.AddNewStudent(student); obj.Save(); } return RedirectToAction("Index", "Student"); } 

This action works without any errors ,but it save an invalid image in App_Data!!!why ?

My Model:

public partial class Student { public Student() { this.Scores = new HashSet<Score>(); } public int Id { get; set; } [Required(ErrorMessage = "نام را وارد کنید")] [DisplayName("نام")] public string Name { get; set; } [Required(ErrorMessage = "نام خانوادگی را وارد کنید")] [DisplayName("نام خانوادگی")] public string LastName { get; set; } [Required(ErrorMessage = "کد ملی را وارد کنید")] [StringLength(10,ErrorMessage = "کد ملی باید ده رقمی باشد",MinimumLength = 10)] [DisplayName("کد ملی")] public string IntNo { get; set; } [Required(ErrorMessage = "نام پدر را وارد کنید")] [DisplayName("نام پدر")] public string FatherName { get; set; } [Required(ErrorMessage = "محل تولد را وارد کنید")] [DisplayName("محل تولد ")] public string BirthLocation { get; set; } [Required(ErrorMessage = "تاریخ تولد را وارد کنید")] [DisplayName("تاریخ تولد")] public string Birthday { get; set; } [Required(ErrorMessage = "آدرس عکس را وارد کنید")] [DisplayName("تصویر")] public string ImageUrl { get; set; } [Required(ErrorMessage = "رشته را وارد کنید")] [DisplayName("رشته")] public string Major { get; set; } [Required(ErrorMessage = "مقطع را وارد کنید")] [DisplayName("مقطع")] public string Degree { get; set; } [Required(ErrorMessage = "شماره شناسنامه را وارد کنید")] [DisplayName("شماره شناسنامه")] public string IdentNo { get; set; } [Required(ErrorMessage = "آدرس را وارد کنید")] [DisplayName("آدرس")] public string Address { get; set; } [Required(ErrorMessage = "شماره همراه را وارد کنید")] [DisplayName("شماره همراه")] public string Mobile { get; set; } [Required(ErrorMessage = "ایمیل را وارد کنید")] [DataType(DataType.EmailAddress,ErrorMessage = "ایمیل نامعتبر است")] [DisplayName("ایمیل")] public string Email { get; set; } [Required(ErrorMessage = "کلمه عبور را وارد کنید")] [DataType(DataType.Password)] [DisplayName("کلمه عبور")] public string Password { get; set; } [DisplayName("تاریخ ثبت نام ")] public System.DateTime RegisterDate { get; set; } [Required(ErrorMessage = "شماره دانشجویی را وارد کنید")] [DisplayName("شماره دانشجویی/دانشجویی")] public string StudentId { get; set; } [Required(ErrorMessage = "وضعیت را وارد کنید")] [DisplayName("وضعیت")] public string State { get; set; } public virtual ICollection<Score> Scores { get; set; } public virtual ICollection<Comment> Comments { get; set; } } 

My image that saved by my code:

enter image description here

Best regards.

Thanks in advance

5
  • student.ImageUrl should be HttpPostedFileBase type, and you cant get this type as string. So, you are wrong to do wrong :) Commented May 9, 2014 at 8:28
  • Share your model and student.ImageUrl output Commented May 9, 2014 at 8:29
  • @AliRızaAdıyahşi i shared it Commented May 9, 2014 at 8:34
  • @AliRızaAdıyahşi can i should change my action to this : HttpPostedFileBase postedFile = Request.Files[0]; string filename = System.IO.Path.GetFileName(Request.Files[0].FileName); string strLocation = HttpContext.Server.MapPath("~/App_Data"); Request.Files[0].SaveAs(strLocation + @"\" + filename); obj.AddNewStudent(student); obj.Save(); Commented May 9, 2014 at 8:36
  • @EA get your file using HttpPostedFileBase save it in the folder then in your imageurl property save the path to your image. Commented May 9, 2014 at 8:44

1 Answer 1

1

Model:

public partial class Student { public Student() { this.Scores = new HashSet<Score>(); } public int Id { get; set; } public HttpPostedFileBase ImageUrl { get; set; } ... 

View: (dont forget enctype="multipart/form-data")

@using (Html.BeginForm("Create", "Home", FormMethod.Post, new { id = "form", enctype="multipart/form-data" })) { <div class="editor-label"> @Html.LabelFor(model => model.ImageUrl) </div> <input id="ImageUrl" title="Upload a student image" type="file" name="ImageUrl" /> ... 

Controller:

... student.ImageUrl.SaveAs("your path here"); ... 

Refrences:

file upload mvc

files upload

Sign up to request clarification or add additional context in comments.

2 Comments

public HttpPostedFileBase ImageUrl { get; set; } ,the c# can resolve the HttpPostedFileBase ,which references should i add ?
And the winner is ... msdn.microsoft.com/en-us/library/… - System.Web

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.