selvi jp

selvi jp

  • NA
  • 323
  • 69.7k

i want add model data with mulitple image

May 8 2021 1:20 PM
i want add model data with mulitple image but  model field are coming null how can i pass model data here
[HttpPost()]
        public string UploadFiles()
        {
            int iUploadedCnt = 0;

            // DEFINE THE PATH WHERE WE WANT TO SAVE THE FILES.
            string sPath = "";
            sPath = System.Web.Hosting.HostingEnvironment.MapPath("~/Images/");

            System.Web.HttpFileCollection hfc = System.Web.HttpContext.Current.Request.Files;

            // CHECK THE FILE COUNT.
            for (int iCnt = 0; iCnt <= hfc.Count - 1; iCnt++)
            {
                System.Web.HttpPostedFile hpf = hfc[iCnt];

                if (hpf.ContentLength > 0)
                {
                    // CHECK IF THE SELECTED FILE(S) ALREADY EXISTS IN FOLDER. (AVOID DUPLICATE)
                    if (!File.Exists(sPath + Path.GetFileName(hpf.FileName)))
                    {
                        // SAVE THE FILES IN THE FOLDER.
                        hpf.SaveAs(sPath + Path.GetFileName(hpf.FileName));
                        iUploadedCnt = iUploadedCnt + 1;
                        //Read the File data from Request.Form collection.
                        HttpPostedFile postedFile = HttpContext.Current.Request.Files[0];

                        //Insert the File to Database Table.
                        WebApiDatabaseEntities entities = new WebApiDatabaseEntities();

                        ImageUpload file = new ImageUpload();
                        ImageUpload model = new ImageUpload();
                        file.Title = model.Title;
                        file.ImagePath = Path.GetFileName(hpf.FileName);
                        file.ImageType = hpf.ContentType;
                        file.CreatedDate = DateTime.Now;
                            
                            entities.ImageUploads.Add(file);
                            entities.SaveChanges();
                         
                    }
                }
            }

            // RETURN A MESSAGE.
            if (iUploadedCnt > 0)
            {
                return iUploadedCnt + " Files Uploaded Successfully";
            }
            else
            {
                return "Upload Failed";
            }
        }