Asp.net mvc上传多张图片靠山存储

Asp.net mvc上传多张图片后台存储
前台页面通过<file name="img">标签数组上传图片,后台根据Request.Files["img"]来接收前台上传的图片。
1
System.Web.HttpFileCollection files = System.Web.HttpContext.Current.Request.Files; 2 if (files.Count == 0) 3 return false; 4 for (int i = 0; i < files.AllKeys.Count(); i++) 5 { 6 if (files.AllKeys[i] != "img") 7 { 8 if (files[i].FileName.Length > 0) 9 { 10 System.Web.HttpPostedFile postedfile = files[i]; 11 string filePath = ""; 12 var ext = Path.GetExtension(postedfile.FileName); 13 var fileName = DateTime.Now.Ticks.ToString() + ext; 14 // 组合文件存储的相对路径 15 filePath = "/Upload/images/" + fileName; 16 // 将相对路径转换成物理路径 17 var path = Server.MapPath(filePath); 18 postedfile.SaveAs(path); 19 string fex = Path.GetExtension(postedfile.FileName); 20 //存储产品轮播图信息 21 ProductCarousel model = new ProductCarousel(); 22 model.ProductID = productID; 23 model.PicSlider = filePath; 24 db.ProductCarousels.Add(model); 25 db.SaveChanges(); 26 } 27 } 28 29 }

 

1楼哓晨