using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Web; using System.Web.SessionState; using Business = SuperMap.RealEstate.Coop.Merchant.Business; namespace Coop.Merchant.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(); Business.IMAGE _IMAGE = new Business.IMAGE(_Transaction); switch (action_type) { case "downloadfile": #region 下载文件 _IMAGE.ResetProperty(); _IMAGE.AddSearchParameter("IMAGE_ID", action_data.ToDecrypt()); if (_IMAGE.Search()) { string uploadPath = HttpContext.Current.Server.MapPath("~/UploadImageDir/PROINST/") + _IMAGE.IMAGE_CONTENT; if (!System.IO.File.Exists(uploadPath)) { context.Response.Write("error:服务器上该文件已被删除或不存在!"); return; } 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=" + (!string.IsNullOrEmpty(_IMAGE.IMAGE_TITLE) ? _IMAGE.IMAGE_TITLE : _IMAGE.IMAGE_CONTENT), 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(); } #endregion break; case "deletefilebycode": _IMAGE.ResetProperty(); _IMAGE.AddSearchParameter("IMAGE_ID", action_data.ToDecrypt()); if (_IMAGE.Search()) { _IMAGE.Delete(); RetString = "1"; } break; } _Transaction.AutoCommit(); context.Response.ContentType = "text/plain"; context.Response.Write(RetString); context.Response.End(); } public bool IsReusable { get { return false; } } } }