<%@ WebHandler Language="C#" Class="GetFile1" %> using System; using System.Collections.Generic; using System.Web; using System.Data; using System.IO; using eMIS.Data; public class GetFile1 : IHttpHandler { public void ProcessRequest (HttpContext context) { string progId = context.Request.QueryString["pid"].ToString(); string recoId = context.Request.QueryString["rid"].ToString(); string idxId = context.Request.QueryString["iid"].ToString(); string elementId = context.Request.QueryString["eid"].ToString(); string fn = (context.Request.QueryString["fn"]!=null ? context.Request.QueryString["fn"].ToString() : "lbxs"); string connectionString = eMIS.Configuration.DataConfig.DefaultConnectionString; eDbManager dbm = new eDbManager(connectionString); eDbOperator ebOperator = dbm.CreateDbOperator(); DataTable dt = ebOperator.ExecuteTable("select a.gjzd,b.dyzd from com_aspsjgl_zb a,com_aspsjgl_cb b where a.id=b.cxid and b." + fn + "=1 and a.id='" + progId + "' and b.kjid='" + elementId + "'"); string f = dt.Rows[0][1].ToString(); string sql = "select " + f + " from " + f.Substring(0, f.IndexOf(".")) + " where " + dt.Rows[0][0].ToString() + "='" + recoId + "'"; dt.Dispose(); dt = ebOperator.ExecuteTable(sql); string filePath = dt.Rows[0][0].ToString(); //context.Response.Write(filePath + "----"); //return; string[] a = filePath.Split('|'); filePath = a[int.Parse(idxId)]; filePath = context.Request.ApplicationPath + "/App_Files/"+ filePath; string fileName = filePath.Substring(filePath.LastIndexOf("/")+1); fileName= HttpUtility.UrlEncode(fileName); fileName = fileName.Replace("+", "%20"); //context.Response.Write(fileName); //return; //string ext = filePath.Substring(filePath.LastIndexOf(".")).ToLower(); //switch (ext) //{ // case "gif": // context.Response.ContentType = "image/gif"; // break; // // case "doc": // context.Response.ContentType = "application/msword"; // break; // default: // context.Response.ContentType = "application/octet-stream"; // break; //} context.Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName.Substring(0,fileName.LastIndexOf("."))); context.Response.ContentType = "application/octet-stream"; //设置下载文件的服务器路径 //输出到流 FileStream stream = new FileStream(context.Server.MapPath(filePath), FileMode.Open, FileAccess.Read, FileShare.Read); BinaryReader reader = new BinaryReader (stream); context.Response.Clear(); while (true) { byte [] bt = reader.ReadBytes(1000); context.Response.OutputStream.Write(bt,0,1000); if(bt.Length<1000) break; } reader.Close(); stream.Dispose(); context.Response.End(); } public bool IsReusable { get { return false; } } }