文件下載
-》使用超鏈接直接指定要下載的文件
能被瀏覽器解析的文會被顯示
不能被瀏覽器解析的文件會被下載
-》實現:無論文件格式,都不使用瀏覽器顯示,完成下載
指向一般處理程序,文件地址作為參數
修改響應頭:ContentType = "application/octet-stream";
設置頭信息:AddHeader("Content-Disposition", "attachment; filename=\"文件名\";");
輸出文件:context.Response.WriteFile(文件地址);
-》提示:如果中文文件名亂碼,可以進行url編碼
HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8);
返回一個字符串,作為文件的名字
public class Download : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
//提示:如果中文文件名亂碼,可以進行url編碼
//HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8);
//返回一個字符串,作為文件的名字
string f = context.Request["f"];
context.Response.ContentType = "application/octet-stream";
context.Response.AddHeader("Content-Disposition", "attachment; filename=\""+f+"\";");//%ab%12
context.Response.WriteFile(AppDomain.CurrentDomain.BaseDirectory+f);
}
public bool IsReusable
{
get
{
return false;
}
}
}
