tinaw25
tinaw25

Reputation: 183

Trouble with foreach loop through image

I am trying to make a foreach loop that is taking all the image, when I debug the code I can see that it takes the images corect but not all is comming to the database. If I choose 3 images only 2 comes in the database and third one is only have the path, but not the image.

Can someone help me.

Here is my code in my Photo model :

public class Photo
{
    public int PhotoId { get; set; }

    public String ProfileImagePath { get; set; }

    public int StudentId { get; set; }

    public virtual Student Student { get; set; }

    public void SaveImage(HttpPostedFileBase image,
        String serverPath, String pathToFile)
    {
        if (image == null) return;

        string filename = Guid.NewGuid().ToString();
        ImageModel.ResizeAndSave(
            serverPath + pathToFile, filename,
            image.InputStream, 200);

        ProfileImagePath = pathToFile + filename +
        ".jpg";
    }


}

And here is my controller code :

[HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include = "StudentId,Name,Adress")] Student student,
        HttpPostedFileBase[] image)
    {
        if (ModelState.IsValid)
        {


            foreach (HttpPostedFileBase file in image)
            {

                string filePathToSave = "/ProfileImages/";
                photo.SaveImage(file, HttpContext.Server.MapPath("~"), "/ProfileImages/");
                photo = new Photo
                {
                    StudentId = student.StudentId,
                    Student = student,
                    ProfileImagePath = filePathToSave
                };
                 db.Photos.Add(photo);
            }
            db.Students.Add(student);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        return View(student);
    }

Upvotes: 0

Views: 136

Answers (1)

user3559349
user3559349

Reputation:

You have not shown the correct code (what you have shown will throw an exception) so I assume you must have declared

Photo photo = new Photo();

somewhere before your photo.SaveImage(..) line which is likely to be the source the error. In addition, your photo.SaveImage() sets the value of ProfileImagePath (to say .../ProfileImages/someGuid.jpg) but then you overwrite it and replace it with just /ProfileImages/ when you call ProfileImagePath = filePathToSave

Change you code to

var path = HttpContext.Server.MapPath("~/ProfileImages"); // only need to set this once
foreach (HttpPostedFileBase file in image)
{
    Photo photo = new Photo
    {
        StudentId = student.StudentId, // no need to set the Student property
    };
    photo.SaveImage(file, path); // this will set the ProfileImagePath property
    db.Photos.Add(photo);
}

and the SaveImage() method to

public void SaveImage(HttpPostedFileBase image, string path)
{
    if (image == null) return;
    string filename = string.Format("{0}.jpg", Guid.NewGuid());
    // not sure what the following line does to it may also need to be modified?
    ImageModel.ResizeAndSave(path, filename, image.InputStream, 200); 
    ProfileImagePath = Path.Combine(path, filename);
}

I would however consider moving the SaveImage() method out of the Photo class and into a separate service, especially as it seems to be calling a static method in another class.

Upvotes: 2

Related Questions