using System; using System.Collections.Generic; using System.Linq; using System.Web; using Business = SuperMap.RealEstate.Personnel.Storage.Business; namespace SuperMap.RealEstate.Personnel.Handler { /// /// Filehelper 的摘要说明 /// public class Filehelper : IHttpHandler { public void ProcessRequest(HttpContext context) { string action_type = string.Empty; string action_data = string.Empty; if (HttpContext.Current.Request.HttpMethod == "GET") { action_type = HttpContext.Current.Request.QueryString["action_type"]; action_data = HttpContext.Current.Request.QueryString["action_data"]; } else { action_type = HttpContext.Current.Request.Form["action_type"]; action_data = HttpContext.Current.Request.Form["action_data"]; } //文件操作 action_data = "24".ToEncrypt(); string RetString = string.Empty; SuperMap.RealEstate.ServiceModel.Transaction _Transaction = new SuperMap.RealEstate.ServiceModel.Transaction(); switch (action_type) { #region 下载文件 case "downloadfile": Business.ATTACHMENT _ATTACHMENT = new Business.ATTACHMENT(_Transaction); _ATTACHMENT.AddSearchParameter("ATTACHMENT_ID", action_data.ToDecrypt()); if (_ATTACHMENT.Search()) { string uploadPath = HttpContext.Current.Server.MapPath("~/UploadImageDir/CompactFile/") + "\\" + _ATTACHMENT.ATTACHMENT_PATH; if (!System.IO.File.Exists(uploadPath)) { context.Response.Write("error:服务器上该文件已被删除或不存在!"); return; } //context.Response.Buffer = true; //context.Response.Clear(); //context.Response.ContentType = "application/download"; //string downFile = _ATTACHMENT.ATTACHMENT_NAME;//这里也可以随便取名 //string EncodeFileName = HttpUtility.UrlEncode(downFile, System.Text.Encoding.UTF8);//防止中文出现乱码 //context.Response.AddHeader("Content-Disposition", "attachment;filename=" + EncodeFileName + ";"); //context.Response.BinaryWrite(System.IO.File.ReadAllBytes(uploadPath));//返回文件数据给客户端下载 //_Transaction.AutoCommit(); //context.Response.Flush(); //context.Response.End(); context.Response.Clear(); context.Response.Buffer = true; context.Response.Charset = "utf-8"; context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");//设置输出流为简体中文 context.Response.AddHeader("Content-Disposition", string.Format("attachment; filename=" + _ATTACHMENT.ATTACHMENT_PATH, DateTime.Now.ToString("yyyyMMddHHmmssfff"))); context.Response.AddHeader("Content-length", System.IO.File.ReadAllBytes(uploadPath).Length.ToString()); //context.Response.BinaryWrite(System.IO.File.ReadAllBytes(uploadPath)); System.IO.FileInfo lFileInfo = new System.IO.FileInfo(uploadPath); context.Response.WriteFile(lFileInfo.FullName); context.Response.ContentType = "application/octet-stream";//设置输出文件类型为excel文件。 context.Response.Flush(); context.Response.End(); } break; #endregion case "deletefilebycode": Business.ATTACHMENT _ATTACHMENT2 = new Business.ATTACHMENT(_Transaction); _ATTACHMENT2.AddSearchParameter("ATTACHMENT_ID", action_data.ToDecrypt()); if (_ATTACHMENT2.Search()) { _ATTACHMENT2.Delete(); RetString = "1"; } break; } _Transaction.AutoCommit(); context.Response.ContentType = "text/plain"; context.Response.Write(RetString); context.Response.End(); } public bool IsReusable { get { return false; } } } }