摘要:ashx檔中使用 BinaryWrite 下載檔案
using System;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.IO;
using System.Configuration;
public class FileHandler : IHttpHandler {
public void ProcessRequest(HttpContext context)
{
try
{
//string Extension=Path.GetExtension("");//取得副檔名
string name = context.Request.QueryString["name"] as string;
context.Response.Buffer = true;
context.Response.Clear();
context.Response.ContentType = "application/download";
context.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(name, System.Text.Encoding.UTF8) + ";");
context.Response.BinaryWrite(File.ReadAllBytes(context.Server.MapPath(string.Format(@"images\{0}", name ) ) ) );
context.Response.Flush();
context.Response.End();
}
catch
{ }
}
public bool IsReusable
{
get
{
return false;
}
}
}
資料來源http://www.dotblogs.com.tw/puma/archive/2009/02/12/7126.aspx