public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; StringBuilder sb = new StringBuilder(); string resultcode = null; string errormsg = null; string paths = ""; string names = ""; string title = ""; string FILEGUID = ""; if (context.Request.Files.Count == 0) { resultcode = "1003"; errormsg = "上传文件为空!"; sb.Append("{"resultcode":" + """ + resultcode + """); sb.Append(","); sb.Append(""errormsg":" + """ + errormsg + """); sb.Append(","); sb.Append(""datas":" + "{"); sb.Append(""FilePath":" + """ + paths + """); sb.Append(","); sb.Append(""FileName":" + """ + names + """); sb.Append(","); sb.Append(""FileTitle":" + """ + title + """); sb.Append(","); sb.Append(""FileGuid":" + """ + FILEGUID + """); sb.Append("}"); sb.Append("}"); } else { string guid = System.Guid.NewGuid().ToString("N"); string dir = context.Server.MapPath("~/TaskMobile/files/") + guid; if (!System.IO.Directory.Exists(dir)) { System.IO.Directory.CreateDirectory(dir); } for (int i = 0; i < context.Request.Files.Count; i++) { //获取上传的文件的对象 HttpPostedFile img = context.Request.Files[i]; //获取上传文件的名称 string s = img.FileName; var strFileExtension = s.Substring(s.LastIndexOf('.') + 1, s.Length - s.LastIndexOf('.') - 1).ToUpper(); String allowextension = System.Configuration.ConfigurationManager.AppSettings["AllowUploadFileType"]; if (allowextension.IndexOf(strFileExtension) >= 0) { int len = s.LastIndexOf(".") - s.LastIndexOf("\") - 1; string fileName = s.Substring(s.LastIndexOf("\") + 1, len); string fullName = s.Substring(s.LastIndexOf("\") + 1); fileName = fileName.Replace(",", ""); fullName = fullName.Replace(",", ""); fullName = fullName.Replace(":", ""); //截取获得上传文件的名称(ie上传会把绝对路径也连带上,这里只得到文件的名称) //string str = System.Guid.NewGuid().ToString("N")+ s.Substring(s.LastIndexOf(".") - 1); // s.Substring(s.LastIndexOf("\") + 1); string path = dir + "\" + fullName; //保存文件 img.SaveAs(path); paths = HttpRuntime.AppDomainAppVirtualPath + "/TaskMobile/files/" + guid + "/" + fullName; title = "/TaskMobile/files/" + guid + "/" + fullName; names = fullName; //附件大写(单位KB) int size = img.ContentLength / 1024; //保存附件到文件表 FILEGUID = new NDFILES().AddFiles(names, "", strFileExtension, NDFILES.getFileType(strFileExtension), size.ToString(), paths, context.User.Identity.Name); resultcode = "1000"; sb.Append("{"resultcode":" + """ + resultcode + """); sb.Append(","); sb.Append(""errormsg":" + """ + errormsg + """); sb.Append(","); sb.Append(""datas":" + "{"); sb.Append(""FilePath":" + """ + paths + """); sb.Append(","); sb.Append(""FileName":" + """ + names + """); sb.Append(","); sb.Append(""FileTitle":" + """ + title + """); sb.Append(","); sb.Append(""FileGuid":" + """ + FILEGUID + """); sb.Append("}"); sb.Append("}"); } else { resultcode = "1003"; errormsg = "不支持的文件类型:" + strFileExtension; sb.Append("{"resultcode":" + """ + resultcode + """); sb.Append(","); sb.Append(""errormsg":" + """ + errormsg + """); sb.Append(","); sb.Append(""datas":" + "{"); sb.Append(""FilePath":" + """ + paths + """); sb.Append(","); sb.Append(""FileName":" + """ + names + """); sb.Append(","); sb.Append(""FileTitle":" + """ + title + """); sb.Append(","); sb.Append(""FileGuid":" + """ + FILEGUID + """); sb.Append("}"); sb.Append("}"); } context.Response.Write(sb.ToString()); } } }