How to uplaod an image using Entity Framework?

时间:2016-10-20 19:14:49

标签: c# asp.net-mvc entity-framework

I'm using ASP.NET C# with entity framework and I'm trying to upload image for a profile and display it.

Here is the relevant part of the View file (Manage.cshtml)

<input type="file" name="form-register-photo" id="form-register-photo" disabled>

Here is the relevant part of the Controller file (Manage.cs)

[HttpPost]
public ActionResult Manage(ManageViewModel manageviewmodel,HttpPostedFileBase upload)
{
    TheFoodyContext db = new TheFoodyContext();
    User user_to_update = db.Users.SingleOrDefault(s => s.email == manageviewmodel.Email);

    if (ModelState.IsValid)
    {
        if (user_to_update != null && (upload != null && upload.ContentLength > 0))
        {

            var fileName = Path.GetFileName(upload.FileName);
            var path = Path.Combine(Server.MapPath("~/FOODY"), fileName);

            user_to_update.email = manageviewmodel.Email;
            user_to_update.fname = manageviewmodel.FirstName;
            user_to_update.lname = manageviewmodel.LastName;
            user_to_update.phone = manageviewmodel.Phone;
            user_to_update.photo = path;
            user_to_update.address = manageviewmodel.Address;
            user_to_update.city = manageviewmodel.City;
            user_to_update.postcode = manageviewmodel.PostCode;
            user_to_update.district = manageviewmodel.District;
            user_to_update.user_type = manageviewmodel.UserType;
            user_to_update.status = manageviewmodel.Status;

            db.SaveChanges();
            return RedirectToAction("Manage");
        }

    }
    return View(manageviewmodel);

}

Within the above controller i have coded for other fields also. So I want to upload the picture among with them. That means from a single button click.

Here is my Model class (ManageViewModel.cs)

public class ManageViewModel
{
    public string FirstName { get; set; }

    public string LastName { get; set; }

    public string Email { get; set; }

    public string Phone { get; set; }

    public string Photo { get; set; }

    public string Address { get; set; }

    public string City { get; set; }

    public int PostCode { get; set; }

    public string District { get; set; }

    public string UserType { get; set; }

    public string Status { get; set; }
}

But for this one photo uploading part did not work properly. So I really don't know how to manage this.

1 个答案:

答案 0 :(得分:1)

Entity Framework doesn't help you to literally upload an image.

  1. From your code, you only just edit 1 record in Users in database, without actually upload the image to hosting drive.

For simple, you will need to have something like below to store the file in physically:

var path = Path.Combine(Server.MapPath("~/FOODY"), fileName);
upload.SaveAs(newSPath);
  1. You didn't show exactly what is the result after this db.SaveChanges(); show I'm not sure whether your photo path getting any error. My suggestion is add in a try catch block and run the code in Debug, see what will you have in user_to_update.photo after db.SaveChanges();