protected void get_download(string fl_path){
try
{
string filepath = fl_path;
string filename = System.IO.Path.GetFileName(filepath);
Response.Clear();
Response.ClearHeaders();
Response.Buffer=false;
Response.ContentType = "application/octet-stream";
//Response.ContentEncoding=System.Text.Encoding.GetEncoding("utf-8");
Response.AddHeader("Content-Disposition", "attachment; filename="+System.Web.HttpUtility.UrlEncode(filename,Encoding.UTF8));
Response.WriteFile(filepath);
Response.Flush();
Response.End();
}
catch(Exception E){
alert("没有找到您所要下载的数据!");
}
}
try
{
string filepath = fl_path;
string filename = System.IO.Path.GetFileName(filepath);
Response.Clear();
Response.ClearHeaders();
Response.Buffer=false;
Response.ContentType = "application/octet-stream";
//Response.ContentEncoding=System.Text.Encoding.GetEncoding("utf-8");
Response.AddHeader("Content-Disposition", "attachment; filename="+System.Web.HttpUtility.UrlEncode(filename,Encoding.UTF8));
Response.WriteFile(filepath);
Response.Flush();
Response.End();
}
catch(Exception E){
alert("没有找到您所要下载的数据!");
}
}
本文介绍了一种在ASP.NET中实现文件下载的方法。通过定义一个受保护的方法get_download,设置HTTP响应头并使用Response对象来读取指定路径的文件内容,从而完成文件下载功能。
212





