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 }

 HBuilder的Uploader插件上传图片到服务器

 1 public JsonResult ApplayCertification()
 2         {
 3             try
 4             {
 5                 var para = Request.Files.Keys;
 6                 foreach (var item in para)
 7                 {
 8                     Log.Debug("所有参数:" + item.ToString());
 9                 }
10                 //接收客户端上传的文件
11                 var FileCollect = HttpContext.Request.Files;
12 
13                 var user = authMag.GetUser(Token);
14                 var certification = db.UserCertifications.SingleOrDefault(p => p.UserID == user.ID);
15 
16                 //遍历文件集合
17                 if (FileCollect.Count > 0)
18                 {
19                     foreach (string key in FileCollect)
20                     {
21                         HttpPostedFileBase FileSave = FileCollect[key];//用key获取单个文件对象HttpPostedFile
22                         //保存上传的图片
23                         string fileName = SaveImg(FileSave, FileCollect.Get(key).FileName, user.ID);
24                         if (key == "OwerImg")
25                         {
26                             certification.OwerImg = fileName;
27                         }
28                         if (key == "OwerCardImg")
29                         {
30                             certification.OwerCardImg = fileName;
31                         }
32                         if (key == "OwerLicenceImg")
33                         {
34                             certification.OwerLicenceImg = fileName;
35                         }
36                         if (key == "OwerDrivingLicenceImg")
37                         {
38                             certification.OwerDrivingLicenceImg = fileName;
39                         }
40                         if (key == "CarImg")
41                         {
42                             certification.CarImg = fileName;
43                         }
44                         if (key == "AssuranceImg")
45                         {
46                             certification.AssuranceImg = fileName;
47                         }
48                     }
49                 }
50                 certification.AuditingStatus = (byte)AuditingStatus.AuditPass;
51                 db.SaveChanges();
52                 return Json(new
53                 {
54                     state = true,
55                     msg = "申请成功",
56                 },
57                 JsonRequestBehavior.AllowGet
58                 );
59             }
60             catch (Exception ex)
61             {
62                 Log.Error("ApplayCertification Error", ex);
63                 return Json(new
64                 {
65                     state = false,
66                 },
67                JsonRequestBehavior.AllowGet
68                );
69             }
70         }

存储上传图片方法

 1  private string SaveImg(HttpPostedFileBase file, string fileName, int userID)
 2         {
 3             //图片路径:Upload+当前日期+当前userID
 4             string filePath = "/Upload/"+DateTime.Now.ToString("yyyy-MM-dd")+"/"+userID.ToString()+"/";
 5             if (!Directory.Exists(Server.MapPath(filePath)))
 6             {
 7                 Directory.CreateDirectory(Server.MapPath(filePath));
 8             }
 9             string returnPath = filePath + DateTime.Now.ToString("yyyyMMddhhmmssffff") + fileName;
10             string absoluteFilePath = Server.MapPath(filePath) + DateTime.Now.ToString("yyyyMMddhhmmssffff") + fileName;
11             file.SaveAs(absoluteFilePath);
12             return returnPath;
13 
14         }
原文地址:https://www.cnblogs.com/CeleryCabbage/p/4933793.html