zeeshan akram

zeeshan akram

  • 1.4k
  • 325
  • 18.2k

Upload multiple Images but store in Database only one!

Feb 22 2020 2:36 AM
How to upload multiple images? 
Hi, I want to upload multiple images in the database base, I select the multiple-image and then upload button but insert in the database only 1 image, any expert can you tell me where I am wrong.
 
 Image Model
  1. public partial class Image  
  2.     {  
  3.         public int imgID { getset; }  
  4.         public string ImgPath { getset; }  
  5.         public long ProdID { getset; }  
  6.           
  7.         public virtual Product Product { getset; }  
  8.     }  
 Controler
  1. public ActionResult AddNewProducts(ProductViewModel prod, List file)  
  2.        {  
  3.            try  
  4.            {  
  5.                List PTlist = _IproductType.PTList();  
  6.                ViewBag.Ptlist = new SelectList(PTlist, "PType_ID""P_Name");  
  7.   
  8.                //Product Color List  
  9.                List pColorList = _IProductColor.PColorlist();  
  10.                ViewBag.pColor_List = new SelectList(pColorList, "C_ID""C_Name_OR_Code");  
  11.   
  12.                List pSizeList = _ISize.pSizeList();  
  13.                ViewBag.pSizeLists = new SelectList(pSizeList, "S_ID""S_Size");  
  14.   
  15.               
  16.            string PathDB = string.Empty;  
  17.                Image img = new Image();  
  18.                foreach (HttpPostedFileBase files in file)  
  19.                {  
  20.                    string filename = Path.GetFileName(files.FileName);  
  21.                    string _filename = DateTime.Now.ToString("yymmssff") + filename;  
  22.                    string extension = Path.GetExtension(files.FileName);  
  23.                    string path = Path.Combine(Server.MapPath("~/Upload/"), _filename);  
  24.                    PathDB = "~/Upload/" + _filename;  
  25.                    if (extension.ToLower() == ".jpeg" || extension.ToLower() == ".jpg" || extension.ToLower() == ".png")  
  26.                    {  
  27.                        if (files.ContentLength <= 1000000)  
  28.                        {  
  29.                            img = new Image();  
  30.                            img.imgID = prod.ImgID;  
  31.                            img.ImgPath = PathDB;  
  32.                            img.ProdID = prod.ProductID;  
  33.   
  34.                        }  
  35.                        else  
  36.                            ViewBag.sizemsg = "Size Limit accessed ";  
  37.                    }  
  38.                    else  
  39.                        ViewBag.fileformat = "File is not Format is not Correct";  
  40.   
  41.                } 
  42. }
 

Answers (1)