<%@ 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;
} }
}
}
}