using System; using System.Configuration; using System.IO; using System.Linq; using System.Net; using System.Text; using System.Web; using Business = SuperMap.RealEstate.Contract.Running.Business; using StorageBusiness = SuperMap.RealEstate.Contract.Storage.Business; using Newtonsoft.Json; namespace SuperMap.RealEstate.Contract.Handler { /// /// ModuleObject_ajax 的摘要说明 /// public class ModuleObject_ajax : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Request.ContentEncoding = Encoding.GetEncoding("UTF-8"); context.Response.ContentEncoding = Encoding.GetEncoding("UTF-8"); context.Response.Charset = "UTF-8"; string RetString = string.Empty; HttpPostedFile file = context.Request.Files["Filedata"]; string Proinst_id = context.Request.Form["PROINST_ID"].ToString(); string uploadtype = context.Request.Form["uploadtype"].ToString(); string attachmentType = context.Request.Form["attachmentType"]; if (file == null) { file = context.Request.Files["UploadFile"]; } double size = GetMB(file.ContentLength); if (size > 10) { RetString = "{\"imageid\" : 0}"; context.Response.Write(RetString); } else { if (Proinst_id == "") return; string ExtraString = System.DateTime.Now.ToString("yyyyMMddHHmmss"); string extension = Path.GetExtension(file.FileName).ToLower(); string filename = Path.GetFileNameWithoutExtension(file.FileName); string uploadPath = "/UploadImageDir/CONTRACTPROINST/"; SuperMap.RealEstate.ServiceModel.Transaction _Transaction = new SuperMap.RealEstate.ServiceModel.Transaction(); if (file != null) { if (extension.ToLower() == ".exe") { RetString = "0"; return; } //获取当前接口url地址 Uri URLAddress = HttpContext.Current.Request.Url; uploadPath += filename + ExtraString + "/"; if (!Directory.Exists(HttpContext.Current.Server.MapPath("~" + uploadPath))) { Directory.CreateDirectory(HttpContext.Current.Server.MapPath("~" + uploadPath)); } file.SaveAs(HttpContext.Current.Server.MapPath("~" + uploadPath + filename + extension)); switch (uploadtype) { case "file_upload_Common": using (Business.ATTACHMENT _ATTACHMENT = new Business.ATTACHMENT(_Transaction)) { _ATTACHMENT.ATTACHMENT_NAME = filename + extension; _ATTACHMENT.OPERATE_DATE = long.Parse(DateTime.Now.ToString("yyyyMMddHHmmss")); _ATTACHMENT.TABLE_ID = int.Parse(Proinst_id); _ATTACHMENT.TABLE_NAME = "T_CONTRACTPROINST"; _ATTACHMENT.ATTACHMENT_PATH = uploadPath + _ATTACHMENT.ATTACHMENT_NAME; _ATTACHMENT.ATTACHMENT_URL = URLAddress.AbsoluteUri + _ATTACHMENT.ATTACHMENT_PATH; _ATTACHMENT.Insert(); string fname = _ATTACHMENT.ATTACHMENT_NAME;//HttpUtility.HtmlEncode(_ATTACHMENT.ATTACHMENT_NAME); RetString = "{\"imageid\" : \"" + _ATTACHMENT.ATTACHMENT_ID_Encrypt + "\", \"imagename\" : \"" + fname + "\"}"; } break; } _Transaction.AutoCommit(); context.Response.ContentType = "text/plain"; //下面这句代码缺少的话,上传成功后上传队列的显示不会自动消失 context.Response.Write(RetString); } else { context.Response.Write(RetString); } } } public bool IsReusable { get { return false; } } #region webUpload使用方法 //WebUpload使用方法 //public void ProcessRequest(HttpContext context) //{ // //string id, string name, string type, string lastModifiedDate, int size, HttpPostedFileBase file // string id = HttpContext.Current.Request.Form["id"]; // string name = HttpContext.Current.Request.Form["name"]; // string type = HttpContext.Current.Request.Form["type"]; // string lastModifiedDate = HttpContext.Current.Request.Form["lastModifiedDate"]; // int size = int.Parse(HttpContext.Current.Request.Form["size"]); // //HttpPostedFileBase _HttpPostedFileBase = HttpContext.Current.Request.Files; // //上传文件 // string action_type = HttpContext.Current.Request.Form["action_type"]; // string reString = string.Empty; // string CurrentGuid = string.Empty; // context.Response.ContentType = "text/plain"; // context.Response.Write(reString); // context.Response.End(); //} /// /// 将B转换为MB /// /// /// private double GetMB(long b) { for (int i = 0; i < 2; i++) { b /= 1024; } return b; } /// /// 文件保存操作 /// /// private void SaveFile(string basePath = "~/Upload/Images/") { var name = string.Empty; basePath = (basePath.IndexOf("~") > -1) ? System.Web.HttpContext.Current.Server.MapPath(basePath) : basePath; HttpFileCollection files = System.Web.HttpContext.Current.Request.Files; //如果目录不存在,则创建目录 if (!Directory.Exists(basePath)) { Directory.CreateDirectory(basePath); } var suffix = files[0].ContentType.Split('/'); //获取文件格式 var _suffix = suffix[1].Equals("jpeg", StringComparison.CurrentCultureIgnoreCase) ? "" : suffix[1]; var _temp = System.Web.HttpContext.Current.Request["name"]; //如果不修改文件名,则创建随机文件名 if (!string.IsNullOrEmpty(_temp)) { name = _temp; } else { Random rand = new Random(24 * (int)DateTime.Now.Ticks); name = rand.Next() + "." + _suffix; } //文件保存 var full = basePath + name; files[0].SaveAs(full); var _result = "{\"jsonrpc\" : \"2.0\", \"result\" : null, \"id\" : \"" + name + "\"}"; System.Web.HttpContext.Current.Response.Write(_result); } #endregion } public class ImageObject { public string imagename { get; set; } public string imageid { get; set; } } }