0

My problem is when I try to upload an image in asp.net mvc, i try this method :

        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/*" >

The problem is the name of the picture is saved in Database but the picture is not saved at the folder named "1", which is the Path.

please any help

Yassine
  • 9
  • 6
  • 1
    Do you [know what a NullReferenceException is and how to fix it](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it)? Did you verify that the HTTP request your browser sends includes the file contents? Did you make sure to declare your HTML inputs as part of a form with the correct enctype? You haven't shown the form declaration in your post, so you don't have a [mcve]. – mason Jun 09 '21 at 12:51
  • I am not sure what this part "~/1" means in your path but please have a look at the answer on this question https://stackoverflow.com/questions/16255882/uploading-displaying-images-in-mvc-4 – Mronzer Jun 09 '21 at 14:24
  • @RonnyMahlangu That would resolve to a directory named `1` in the root of the web application directory. The Path.Combine would then add the filename to it. Most likely that code is fine, except a check is needed to make sure the HttpPostedFileBase parameter isn't null. – mason Jun 09 '21 at 16:04
  • @mason that make sense, then in that case he needs to add a line that will check if the directory named 1 exists(create it if it does not) then also do the null check on the file like you pointed out. – Mronzer Jun 10 '21 at 06:03
  • @Ronny Yes that's right, and i created the folder myself, so i don't have to make it created by a function or something, – Yassine Jun 10 '21 at 09:07
  • @Ronny what check should i also have to do (null check) ? – Yassine Jun 10 '21 at 09:08
  • @Yassine you need to do a null check on your file صورة, would you please send us the form tag of your view, by form tag I am referring to something like Html.BeginForm("Post", "Home", FormMethod.Post, new {enctype = "multipart/form-data"})) – Mronzer Jun 10 '21 at 09:16
  • @Ronny yes it is like this Html.BeginForm("Post", "Home", FormMethod.Post, new {enctype = "multipart/form-data"})) – Yassine Jun 10 '21 at 09:18
  • now i'm trying to test the link you mentioned – Yassine Jun 10 '21 at 09:19
  • @Yassine I would suggest that you edit your question and add the above part as part of your question as it is difficult to read it from a comment – Mronzer Jun 10 '21 at 09:31
  • Maybe this could also help: https://stackoverflow.com/questions/7656467/httppostedfilebase-saveas-working-but-no-file-uploaded-and-no-exceptions – Patrick Xavier Silerio Jun 11 '21 at 02:41

1 Answers1

0
 if (PersonalImage != null && PersonalImage.ContentLength > 0) 
            {
                string _ImageName = Path.GetFileName(_random + "-" + lastname + "-" + PersonalImage.FileName);
                string _Path = Path.Combine(Server.MapPath("~/Content/Uploads/Adherentes/Personalimages"), _ImageName);
                PersonalImage.SaveAs(_Path);
                adherent.PersonalImage = _ImageName;
            }
Yassine
  • 9
  • 6