前台頁面通過<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 }