using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Web; using Spire.Doc; using Spire.Doc.Documents; using Business = SuperMap.RealEstate.MainTenance.Storage.Business; using StorageBusiness = SuperMap.RealEstate.HighWay.Storage.Business; namespace SuperMap.RealEstate.MainTenance.Storage.Handler { /// /// Handler1 的摘要说明 /// 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"]; string uploadtype = context.Request.Form["uploadtype"]; string other_name = ""; if (!string.IsNullOrEmpty(context.Request.Form["OTHER_NAME"])) { other_name = context.Request.Form["OTHER_NAME"].ToString(); } if (string.IsNullOrWhiteSpace(Proinst_id)) { return; } //string uploadPath = uploadtype == "contract_upload" ? HttpContext.Current.Server.MapPath( // "~/UploadImageDir/ContractTemplate/") + "\\" : HttpContext.Current.Server.MapPath("~/UploadImageDir/CompactFile/") + "\\"; string ExtraString = System.DateTime.Now.ToString("yyyyMMddHHmmss"); string extension = Path.GetExtension(file.FileName).ToLower(); string filename = Path.GetFileName(file.FileName).Replace(extension, ""); string uploadPath = "", imagetype = "0"; if (extension.ToLower() == ".exe") { RetString = "0"; return; } #region 文件存储路径 switch (uploadtype) { //设备维护 case "upload_files": uploadPath = HttpContext.Current.Server.MapPath("~/UploadImageDir/Maintain/") + ExtraString + "_" + filename + "/"; break; } #endregion SuperMap.RealEstate.ServiceModel.Transaction _Transaction = new SuperMap.RealEstate.ServiceModel.Transaction(); if (file != null) { ImageObject _ImageObject = new ImageObject(); if (!Directory.Exists(uploadPath)) { Directory.CreateDirectory(uploadPath); } string webFilePath = uploadPath + filename + extension; // 服务器端文件路径 file.SaveAs(uploadPath + filename + extension); if (uploadtype == "contract_upload") { if (File.Exists(webFilePath)) { try { //file.SaveAs(webFilePath); // 使用 SaveAs 方法保存文件 Spire.Doc.Document doc = new Spire.Doc.Document(); doc.LoadFromFile(webFilePath); PictureWatermark picture = new PictureWatermark(); picture.Picture = System.Drawing.Image.FromFile(HttpContext.Current.Server.MapPath( "~/CreateWord/WaterMarkImage/watermark.png")); picture.Scaling = 80; doc.Watermark = picture; doc.SaveToFile(webFilePath); } catch (Exception ex) { SuperMap.RealEstate.Utility.ErrorLogHelper.Write(ex); } } } #region 文件路径存储 switch (uploadtype) { case "upload_files": if (extension != ".exe") { using (Business.IMAGE _IMAGE = new Business.IMAGE(_Transaction)) { //_IMAGE.IMAGE_URL = filename + ExtraString + extension; _IMAGE.IMAGE_DESC = filename + extension; _IMAGE.IMAGE_PATH = "/Maintain/" + "" + ExtraString + "_" + filename; _IMAGE.IMAGE_DATE = DateTime.Now; _IMAGE.TABLE_ID = int.Parse(Proinst_id); _IMAGE.TABLE_NAME = "T_MAINTAIN"; _IMAGE.Insert(); string fname = _IMAGE.IMAGE_URL;//HttpUtility.HtmlEncode(_ATTACHMENT.ATTACHMENT_NAME); RetString = "{\"imageid\" : \"" + _IMAGE.IMAGE_ID_Encrypt + "\", \"imagename\" : \"" + fname + "\"}"; } } else { RetString = "0"; } break; } #endregion _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(); //} /// /// 文件保存操作 /// /// 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 } #region 图片对象 public class ImageObject { public string imagename { get; set; } public string imageid { get; set; } } #endregion }