using System; using System.Configuration; using System.IO; using System.Linq; using System.Net; using System.Text; using System.Web; using Business = SuperMap.RealEstate.Finance.Running.Business; using StorageBusiness = SuperMap.RealEstate.Finance.Storage.Business; using Newtonsoft.Json; namespace SuperMap.RealEstate.Finance.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 = HttpContext.Current.Server.MapPath("~/UploadImageDir/FINANCEPROINST/") + "\\"; SuperMap.RealEstate.ServiceModel.Transaction _Transaction = new SuperMap.RealEstate.ServiceModel.Transaction(); if (file != null) { if (extension.ToLower() == ".exe") { RetString = "0"; return; } //string filePath = "D:/FINANCEPROINST/" + DateTime.Now.ToString("yyyyMMddHHmmssff") + "_" + filename; //string fileType = file.ContentType; //Stream stream = file.InputStream; //byte[] data = new byte[stream.Length]; //stream.Read(data, 0, data.Length); //string fileSize = data.Length.ToString(); //HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://10.104.1.8:8010/publish/FileUpload.ashx"); //myRequest.Method = "POST"; //myRequest.ContentType = fileType; //myRequest.ContentLength = data.Length; //myRequest.Headers.Add("FileType", HttpContext.Current.Server.UrlEncode(fileType)); //myRequest.Headers.Add("FileSize", fileSize); //myRequest.Headers.Add("FileName", HttpContext.Current.Server.UrlEncode(file.FileName)); //myRequest.Headers.Add("FileDir", HttpContext.Current.Server.UrlEncode(@filePath)); //using (Stream newStream = myRequest.GetRequestStream()) //{ // // Send the data. // newStream.Write(data, 0, data.Length); // newStream.Close(); //} //HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse(); //StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8); //string content = reader.ReadToEnd(); //获取当前接口url地址 Uri URLAddress = HttpContext.Current.Request.Url; string FileUrl = ConfigurationManager.AppSettings["FinanceUrl"] + "/Finance/Compents/uploadData/DownloadFilePage.aspx"; if (uploadtype == "file_upload_Common") { uploadPath += filename + ExtraString + "\\"; if (!Directory.Exists(uploadPath)) { Directory.CreateDirectory(uploadPath); } file.SaveAs(uploadPath + filename + extension); } else { if (!Directory.Exists(uploadPath)) { Directory.CreateDirectory(uploadPath); } file.SaveAs(uploadPath + filename + ExtraString + extension); } switch (uploadtype) { case "file_upload_Common": Common.ATTACHMENTModel attachmentmodel = new Common.ATTACHMENTModel(); attachmentmodel.ATTACHMENT_NAME = filename + extension; attachmentmodel.PRESONNAME = filename + ExtraString; attachmentmodel.CREATEDATE = DateTime.Now; attachmentmodel.PROINST_ID = int.Parse(Proinst_id); attachmentmodel.ATTACHMENT_DESC = !string.IsNullOrEmpty(attachmentType) ? attachmentType : ""; attachmentmodel.OTHER_NAME = FileUrl; string postUrl = ConfigurationManager.AppSettings["EShangApiMainUrls"] + "/Finance/SynchroATTACHMENT"; string result = Common.Common.HttpUrlPost(JsonConvert.SerializeObject(attachmentmodel), postUrl, "application/json"); //_ATTACHMENT.Insert(); RetString = "{\"imageid\" : \"" + attachmentmodel.ATTACHMENT_ID.ToEncrypt() + "\", \"imagename\" : \"" + attachmentmodel.ATTACHMENT_NAME + "\"}"; break; case "proinst_upload": case "proinst_upload1000": case "proinst_upload2000": case "proinst_upload3000": case "proinst_upload4000": using (Business.ATTACHMENT _ATTACHMENT = new Business.ATTACHMENT(_Transaction)) { _ATTACHMENT.ATTACHMENT_NAME = filename + ExtraString + extension; _ATTACHMENT.CREATEDATE = DateTime.Now; _ATTACHMENT.PROINST_ID = int.Parse(Proinst_id); _ATTACHMENT.ATTACHMENT_DESC = !string.IsNullOrEmpty(attachmentType) ? attachmentType : ""; _ATTACHMENT.OTHER_NAME = FileUrl; _ATTACHMENT.Insert(); string fname = _ATTACHMENT.ATTACHMENT_NAME;//HttpUtility.HtmlEncode(_ATTACHMENT.ATTACHMENT_NAME); RetString = "{\"imageid\" : \"" + _ATTACHMENT.ATTACHMENT_ID_Encrypt + "\", \"imagename\" : \"" + fname + "\"}"; } break; case "finance_upload": using (StorageBusiness.ATTACHMENT _ATTACHMENT = new StorageBusiness.ATTACHMENT(_Transaction)) { _ATTACHMENT.ATTACHMENT_NAME = filename + ExtraString + extension; _ATTACHMENT.CREATEDATE = DateTime.Now; _ATTACHMENT.PROINST_ID = int.Parse(Proinst_id); _ATTACHMENT.OTHER_NAME = FileUrl; _ATTACHMENT.Insert(); string fname = _ATTACHMENT.ATTACHMENT_NAME;//HttpUtility.HtmlEncode(_ATTACHMENT.ATTACHMENT_NAME); RetString = "{\"imageid\" : \"" + _ATTACHMENT.ATTACHMENT_ID_Encrypt + "\", \"imagename\" : \"" + fname + "\"}"; } break; case "upload_files": using (StorageBusiness.IMAGE _IMAGE = new StorageBusiness.IMAGE(_Transaction)) { _IMAGE.IMAGE_URL = FileUrl; _IMAGE.IMAGE_DATE = DateTime.Now; _IMAGE.TABLE_ID = int.Parse(Proinst_id); _IMAGE.TABLE_NAME = "T_DOCUMENT"; _IMAGE.Insert(); string fname = _IMAGE.IMAGE_URL;//HttpUtility.HtmlEncode(_ATTACHMENT.ATTACHMENT_NAME); RetString = "{\"imageid\" : \"" + _IMAGE.IMAGE_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; } } }