-1

I use this method to upload image and save it to a folder i created in the server :

public ActionResult Create(Regional regional, HttpPostedFileBase صورة)
{               

            if (ModelState.IsValid)
            {
  if (صورة != null)
                {
                    صورة.SaveAs(Path.Combine(Server.MapPath("~/1"), صورة.FileName));
                    regional.صورة = صورة.FileName;
                }

                db.Regionals.Add(regional);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(regional);
        }

and the input is :

<input type="file" id="صورة" name="صورة" accept="image/*" >

But The problem is when i press submit, the name of the image is saved in Database but the picture is not saved to the sever, i can't find it in the folder that i created which is named "1"

please any help

Yassine
  • 9
  • 6
  • `regional.صورة` is set with `FileName`, not the path. Maybe you prefix with the folder, like `regional.صورة = Path.Combine("1", صورة.FileName);` – vernou Jun 10 '21 at 10:12
  • @vernou it's not working either :( – Yassine Jun 10 '21 at 10:24
  • Can you explain "the picture is not saved at the folder named "1", which is the Path."? I don't understand this part. You can edit your question. – vernou Jun 10 '21 at 10:30
  • @Vernou i did sir – Yassine Jun 10 '21 at 10:33
  • Maybe this can help : https://stackoverflow.com/questions/275781/server-mappath-server-mappath-server-mappath-server-mappath – vernou Jun 10 '21 at 10:48

1 Answers1

0

Try this:

        public ActionResult Create(Regional regional, HttpPostedFileBase صورة)
        {               
        if (ModelState.IsValid)
        {
           if (صورة != null)
            {
           var fileName = Path.GetFileName(صورة.FileName); 
                صورة.SaveAs(Path.Combine(Server.MapPath("~/1"),filename));
                regional.صورة = صورة.FileName;
            }

             db.Regionals.Add(regional);
             db.SaveChanges();
             return RedirectToAction("Index");
        }
        return View(regional);
        }
Naeem_VE
  • 94
  • 3