<%@ WebHandler Language="C#" %> 

 using System; 

 using System.Web; 

 using System.Data.SqlClient; 

 using System.Data; 

 using System.IO; 

 using System.Configuration; 

 public class filedownload : IHttpHandler 

 { 

  public void ProcessRequest(HttpContext context) 

 { 

 string guid = context.Request.QueryString["guid"] as string; 

 //讀取DB 

 using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString)) 

 { 

 string sql = "select * from [fileupload] where guid=@guid"; 

 SqlCommand cmd = new SqlCommand(sql, conn); 

 cmd.Parameters.Add("guid", SqlDbType.Char, 36).Value = guid; 

 conn.Open(); 

 SqlDataReader dr = cmd.ExecuteReader(); 

 if (dr.Read()) 

 { 

 string filename = dr["filename"].ToString(); 

 context.Response.Buffer = true; 

 context.Response.Clear(); 

 context.Response.ContentType = "application/download"; 

 context.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8) + ";"); 

 context.Response.BinaryWrite(File.ReadAllBytes(context.Server.MapPath(string.Format(@"file\{0}.{1}", guid, Path.GetExtension(filename))))); 

 context.Response.Flush(); 

 context.Response.End(); 

 } 

 dr.Close(); 

 } 

 } 

 

 public bool IsReusable 

 { 

 get

 { 

 return false; 

 }  } 

 } 
    }
}

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 AndrewHsiao 的頭像
    AndrewHsiao

    Andrew的部落格

    AndrewHsiao 發表在 痞客邦 留言(0) 人氣()