1 protected void Page_Load(object sender, EventArgs e) 2 { 3 4 string BelongedTo = Request["BelongedTo"].ToString(); 5 string RecordNumber = Request["RecordNumber"].ToString(); 6 string ApplyNum = Request["ApplyNum"].ToString(); 7 string PhotoType = Request["PhotoType"].ToString(); 8 string UploadTerminal = Request["UploadTerminal"].ToString(); 9 try 10 { 11 HttpFileCollection files = HttpContext.Current.Request.Files; 12 if (files == null) return; 13 for (int i = 0; i < files.Count; i++) 14 { 15 HttpPostedFile item = files[i]; 16 if (item.ContentLength > 1024 * 1024) 17 { 18 Response.Write("{'failure':'图片大小不能超过1M'}"); 19 return; 20 } 21 else if (item.ContentLength < 50 * 1024) 22 { 23 Response.Write("{'failure':'图片大小不能小于50KB'}"); 24 return; 25 } 26 string fileOldName = item.FileName; 27 string fileSuffixes = fileOldName.Substring(fileOldName.LastIndexOf(".") + 1).Trim(); 28 string fileNewName = DateTime.Now.Ticks.ToString() + new Random().Next(1000, 9999).ToString() + "." + fileSuffixes; 29 item.SaveAs(HttpContext.Current.Request.MapPath("../../../../upload/")+ fileNewName); 30 31 ProjectOverviewDAL.AddCrateTargetImg(new ProTargetUpdatePhotoEntity { 32 BelongedTo = BelongedTo, 33 RecordNumber=RecordNumber, 34 ApplyNum = ApplyNum, 35 PhotoType = PhotoType, 36 UploadTerminal = UploadTerminal, 37 PhotoUrl="../../../../upload/"+ fileNewName 38 }); 39 } 40 } 41 catch (Exception ex) 42 { 43 Response.Write("{'success':'false', 'files':'文件上传失败: " + ex.Message + "'}"); 44 Response.End(); 45 } 46 47 Response.Write("{'success':'true'}"); 48 Response.End(); 49 }