-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

5
  • regional.صورة is set with FileName, not the path. Maybe you prefix with the folder, like regional.صورة = Path.Combine("1", صورة.FileName);
    – vernou
    CommentedJun 10, 2021 at 10:12
  • @vernou it's not working either :(
    – Yassine
    CommentedJun 10, 2021 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
    CommentedJun 10, 2021 at 10:30
  • @Vernou i did sir
    – Yassine
    CommentedJun 10, 2021 at 10:33
  • Maybe this can help : stackoverflow.com/questions/275781/…
    – vernou
    CommentedJun 10, 2021 at 10:48

1 Answer 1

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); } 
3
  • Thank you for your answer firstly, but i tried and the same problem which is the image is not uploaded to the server, just its name to database
    – Yassine
    CommentedJun 10, 2021 at 10:53
  • Try to use only Server.MapPath ex. صورة.SaveAs(Server.MapPath("~/1"),filename);
    – Naeem_VE
    CommentedJun 10, 2021 at 12:04
  • i can't because saveas can't be applied to server.mappath method
    – Yassine
    CommentedJun 10, 2021 at 12:26

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.