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
- public partial class Image
- {
- public int imgID { get; set; }
- public string ImgPath { get; set; }
- public long ProdID { get; set; }
-
- public virtual Product Product { get; set; }
- }
Controler
- public ActionResult AddNewProducts(ProductViewModel prod, List file)
- {
- try
- {
- List PTlist = _IproductType.PTList();
- ViewBag.Ptlist = new SelectList(PTlist, "PType_ID", "P_Name");
-
-
- List pColorList = _IProductColor.PColorlist();
- ViewBag.pColor_List = new SelectList(pColorList, "C_ID", "C_Name_OR_Code");
-
- List pSizeList = _ISize.pSizeList();
- ViewBag.pSizeLists = new SelectList(pSizeList, "S_ID", "S_Size");
-
-
- string PathDB = string.Empty;
- Image img = new Image();
- foreach (HttpPostedFileBase files in file)
- {
- string filename = Path.GetFileName(files.FileName);
- string _filename = DateTime.Now.ToString("yymmssff") + filename;
- string extension = Path.GetExtension(files.FileName);
- string path = Path.Combine(Server.MapPath("~/Upload/"), _filename);
- PathDB = "~/Upload/" + _filename;
- if (extension.ToLower() == ".jpeg" || extension.ToLower() == ".jpg" || extension.ToLower() == ".png")
- {
- if (files.ContentLength <= 1000000)
- {
- img = new Image();
- img.imgID = prod.ImgID;
- img.ImgPath = PathDB;
- img.ProdID = prod.ProductID;
-
- }
- else
- ViewBag.sizemsg = "Size Limit accessed ";
- }
- else
- ViewBag.fileformat = "File is not Format is not Correct";
-
- }
- }
- }