1 <table class="table table-strippled exhibit-table"> 2 <tbody> 3 <tr> 4 <td class="background-title"><i class="red-font"></i><span class="background-title"> 备份文件</span></td> 5 <td> 6 <form action="#" class="clearfix" id="uploadForm" method="post"> 7 <input id="bkImg" type='file' name="files" 8 class="file" data-min-file-count="2" onchange="FileChange('bkImg', 'uploadForm')"> 9 </form> 10 </td> 11 12 </tr> 13 </tbody> 14 </table>
1 //图片上传 2 var nameImg = "";//定义全局变量存储文件名 3 function FileChange(inputId, uploadForm) { 4 if ($("#" + inputId).val() == "") { 5 return; 6 } 7 else { 8 var formData = new FormData($("#" + uploadForm)[0]); 9 $.ajax({ 10 url: "/CContractMgr/SubmitImg", 11 type: "POST", 12 data: formData, 13 async: false, 14 cache: false, 15 contentType: false, 16 processData: false, 17 success: function (result) { 18 if (result.r > 0) { 19 nameImg = result.nameImg;//获取返回的文件名 20 } else { 21 if (result.message.trim() == "") { result.message = "系统错误,请重新操作!"; } 22 layer.alert(result.message); 23 $("#" + inputId).val(""); 24 } 25 } 26 }); 27 } 28 }
1 /// <summary> 2 /// 上传图片 3 /// </summary> 4 /// <returns></returns> 5 [HttpPost] 6 public JsonResult SubmitImg() 7 { 8 var _r = 0; 9 var _message = ""; 10 var _nameImg = ""; 11 //上传和返回(保存到数据库中)的路径 12 string uppath = string.Empty; 13 string savepath = string.Empty; 14 HttpPostedFileBase imgFile = Request.Files[0]; 15 if (Request.Files.Count > 0) 16 { 17 18 if (imgFile != null) 19 { 20 //创建图片新的名称 21 string nameImg = DateTime.Now.ToString("yyyyMMddHHmmssfff"); 22 //获得上传图片的路径 23 string strPath = imgFile.FileName; 24 //获得上传图片的类型(后缀名) 25 string type = strPath.Substring(strPath.LastIndexOf(".") + 1).ToLower(); 26 if (ValidateImg(type)) 27 { 28 //拼写上传图片的路径 29 var ImgUrl = "/Images/contract/contractImg/"; 30 uppath = Server.MapPath(ImgUrl); 31 if (!Directory.Exists(uppath))//判断上传文件夹是否存在,若不存在,则创建 32 { 33 Directory.CreateDirectory(uppath);//创建文件夹 34 } 35 uppath += nameImg + "." + type;//图片保存路径 36 37 //拼写数据库保存的相对路径字符串 38 savepath += ImgUrl + nameImg + "." + type; 39 40 //上传图片 41 try 42 { 43 imgFile.SaveAs(uppath); 44 _r = 1; 45 _message = "上传成功"; 46 _nameImg = nameImg; 47 } 48 catch (Exception ex) 49 { 50 UtilityHelp.WriteLog(ex, ex.Message); 51 _r = 0; 52 _message = "上传异常!"; 53 } 54 } 55 else 56 { 57 _r = 0; 58 _message = "合同备份仅支持PDF格式,请加密后重新上传!"; 59 } 60 } 61 } 62 var json = new { r = _r, filename = savepath, message = _message, nameImg = _nameImg }; 63 return Json(json, JsonRequestBehavior.AllowGet); 64 } 65 66 /// <summary> 67 /// 图片上传类型 68 /// </summary> 69 /// <param name="imgName"></param> 70 /// <returns></returns> 71 public bool ValidateImg(string imgName) 72 { 73 string[] imgType = new string[] { "pdf" }; 74 75 int i = 0; 76 bool blean = false; 77 string message = string.Empty; 78 79 //判断是否为Image类型文件 80 while (i < imgType.Length) 81 { 82 if (imgName.Equals(imgType[i].ToString())) 83 { 84 blean = true; 85 break; 86 } 87 else if (i == (imgType.Length - 1)) 88 { 89 break; 90 } 91 else 92 { 93 i++; 94 } 95 } 96 return blean; 97 }