1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using LitJson; 6 using LY.CCPC.Model; 7 using System.IO; 8 using System.Reflection; 9 using System.Data; 10 using System.Configuration; 11 using System.Collections.Specialized; 12 using LY.CCPC.BLL; 13 using LY.CCPCUEP.Utility; 14 15 16 namespace LY.CCPCUEP.UI.Handlers 17 { 18 /// <summary> 19 /// ImportFileHandler 的摘要说明 20 /// </summary> 21 public class ImportFileHandler : IHttpHandler 22 { 23 public void ProcessRequest(HttpContext context) 24 { 25 JsonObject json = new JsonObject() { Success = true }; 26 27 string fullMethod = context.Request.QueryString["mod"]; 28 string strType = context.Request.QueryString["type"]; 29 string fullFileName = string.Empty; 30 FTPHelper ftp=getFtpPath(); 31 try 32 { 33 HttpFileCollection fileColl = context.Request.Files; 34 string empid = context.User.Identity.Name; 35 if ("UPLOAD".Equals(fullMethod.ToUpper())) 36 { 37 HttpPostedFile file = fileColl[0];//得到上传的文件 38 39 //1.判断文件扩展名是否符合要求 40 string org_file_name = getOrgFileName(file.FileName);//初始文件名 41 string ext = getFileExtName(file.FileName); 42 ext = ext.ToLower(); 43 string[] exts = new string[] { ".jpg", ".ppt", ".pptx", ".bmp", ".gif", ".png", ".jpeg", ".txt", ".csv", ".xls", ".xlsx", ".doc", ".docx", ".pdf", ".avi ", ".rmvb ", ".rm ", ".asf ", ".divx ", ".mpg ", ".mpeg ", ".mpe ", ".wmv ", ".mp4 ", ".mkv ", ".vob", ".rar", ".zip" }; 44 if (!exts.Contains(ext)) 45 { 46 json.Success = false; 47 json.Data = ""; 48 json.Message = string.Format("系统不允许上传类型为{0}的文件,请重新选择上传!", ext); 49 // context.Response.Write(JsonMapper.ToJson(json)); 50 return; 51 } 52 53 //2.判断文件大小,不能超过5MB 54 int fileSize = file.ContentLength; 55 const int MB = 1024 * 1024; 56 57 if (fileSize / MB >= 5) 58 { 59 json.Success = false; 60 json.Data = ""; 61 json.Message = "上传的文件不能超过5MB,请重新选择上传!"; 62 //context.Response.Write(JsonMapper.ToJson(json)); 63 return; 64 } 65 66 string filePath = this.getLocalPath("1"); 67 string uploadPath = context.Server.MapPath("~" + filePath) ;//导入文件路径 68 string fileName = ""; 69 70 fileName = DateTime.Now.ToString("yyyyMMddHHmmssfff")+ ext;//文件名 71 if (!Directory.Exists(uploadPath)) 72 { 73 Directory.CreateDirectory(uploadPath); 74 } 75 fullFileName = uploadPath +"\\"+ fileName; 76 file.SaveAs(fullFileName);//文件在服务器的全路径 77 78 ftp.Upload(fullFileName); // 上传到FTP服务器 79 string file_path = ftp.ftpRemotePath.Substring(ftp.ftpRemotePath.IndexOf('/')) ;//保存在服务器的路径 80 string org_file_path = (file_path + "\\" + fileName).Replace("\\", "/"); 81 org_file_name = org_file_name.Substring(org_file_name.LastIndexOf("\\") + 1); 82 string strMsg = "{\"file_path\":\""+org_file_path+"\",\"file_name\":\""+org_file_name+"\"}"; 83 //string strMsg = string.Format("[{\"file_path\":\"{0}\"},{\"file_name\":\"{1}\"}]", , ); 84 json.Data = strMsg; 85 86 } 87 } 88 catch (Exception ex) 89 { 90 json.Success = false; 91 json.Message = ex.Message; 92 json.Data = ex.Message + "###上传FTP路径:" + ftp.ftpURI; 93 } 94 finally 95 { 96 string result = JsonMapper.ToJson(json); 97 //删除AP上的临时文件 98 if (File.Exists(fullFileName)) 99 { 100 File.Delete(fullFileName); 101 } 102 context.Response.Write(result); 103 context.Response.End(); 104 } 105 } 106 107 private string getLocalPath(string flag) 108 { 109 string path = string.Empty; 110 111 string ftpPath = ConfigurationManager.AppSettings["FTP"]; 112 string filePath = ConfigurationManager.AppSettings["uploadPath"]; 113 string ImportPath = ConfigurationManager.AppSettings["ImportPath"]; 114 string ExportPath = ConfigurationManager.AppSettings["ExportPath"]; 115 if ("1".Equals(flag)) 116 { 117 path = ftpPath + filePath; 118 } 119 else if ("2".Equals(flag)) 120 { 121 path = ftpPath + ImportPath; 122 } 123 else if ("3".Equals(flag)) 124 { 125 path = ftpPath + ExportPath; 126 } 127 return path; 128 } 129 /// <summary> 130 /// 返回FTP对像 131 /// </summary> 132 /// <returns></returns> 133 private FTPHelper getFtpPath() 134 { 135 136 string ftpPath = ConfigurationManager.AppSettings["FTPServer"]; 137 string filePath = ConfigurationManager.AppSettings["FTPPath"]; 138 string ftpUser = ConfigurationManager.AppSettings["FTPUser"]; 139 string ftpPass = ConfigurationManager.AppSettings["FTPPass"]; 140 141 return new FTPHelper(ftpPath,filePath,ftpUser,ftpPass); 142 } 143 144 /// <summary> 145 /// 获取文件扩展名 146 /// </summary> 147 /// <param name="fileName"></param> 148 /// <returns></returns> 149 private string getFileExtName(string fileName) 150 { 151 if (string.IsNullOrEmpty(fileName)) return string.Empty; 152 if (!fileName.Contains(".")) return fileName; 153 int posIndex = fileName.LastIndexOf('.'); 154 return fileName.Substring(posIndex); 155 } 156 /// <summary> 157 /// 获取文件原始名 158 /// </summary> 159 /// <param name="fileName"></param> 160 /// <returns></returns> 161 private string getOrgFileName(string fileName) 162 { 163 if (string.IsNullOrEmpty(fileName)) return string.Empty; 164 if (!fileName.Contains("/")) 165 return fileName; 166 int posIndex = fileName.LastIndexOf('/')+1; 167 return fileName.Substring(posIndex); 168 } 169 public bool IsReusable 170 { 171 get 172 { 173 return false; 174 } 175 } 176 177 } 178 }