Asp.netmvc 여러 장의 사진 백엔드 메모리 업로드

10192 단어
      <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         }

좋은 웹페이지 즐겨찾기