Click here to Skip to main content
15,879,474 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi everyone,
I am trying to save image path in the database and in the root folder. but it always pass the null value. please see my code below...

I will really appreciate any help... thanks in advance

What I have tried:

<pre> [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult UploadImage(ImageModel collection,HttpPostedFileBase uploadFile)
        {
          

            if (uploadFile != null && uploadFile.ContentLength > 0)
            {
                string imgname = Path.GetFileName(uploadFile.FileName);
                string imgext = Path.GetExtension(imgname);

                if (imgext == ".jpg" || imgext == ".png")
                {
                    string imgPath = Path.Combine(Server.MapPath("~/gallery/"), imgname);

                    int success = 0;

                    ImageModel imgmodel = new ImageModel();
                    imgmodel.img_ext = imgext;
                    imgmodel.img_url = imgPath;
                    imgmodel.imgName = imgname;
                    success = IImageRep.AddImage(imgmodel);
                    uploadFile.SaveAs(imgPath);
                }
            }


            return View();
            
          
        }



view-------


@using (Html.BeginForm("UploadImage", "Image", FormMethod.Post, new { entype = "multipart/form-data" }))
{

    @Html.AntiForgeryToken()

    <h4>tbl_Images</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })


<input type="file" name="uploadFile" id="uploadFile" />

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Create" class="btn btn-default" />
        </div>

    </div>
}
Posted
Updated 25-Mar-21 8:45am

1 solution

I got that working, there was typo issue...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900