1. 程式人生 > >上傳文件到服務器

上傳文件到服務器

ast pat action hid 全局 table b- mss message

技術分享
 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>
HTML中上傳框 技術分享
 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 }
JS 技術分享
 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         }
Controller控制器:

上傳文件到服務器