asp.net文件下载的几种方式
摘自:http://zhidao.baidu.com/question/171876794.html
阅:经验证,//使用WriteFile下载文件 这个方法,是可以用的;
asp.net2.0以后,我们有了很多种文件下载的选择。
public partial class FileDownLoad : System.Web.UI.Page
{
//提供下载的文件,不编码的话文件名会乱码
private string fileName = HttpContext.Current.Server.UrlEncode("规范.rar");
private string filePath = HttpContext.Current.Server.MapPath("规范.rar");
//使用TransmifFile下载文件
protected void btnDL1_Click(object sender, EventArgs e)
{
FileInfo info = new FileInfo(filePath);
long fileSize = info.Length;
Response.Clear();
Response.ContentType = "application/x-zip-compressed";
Response.AddHeader("Content-Disposition", "attachment;filename="+ fileName);
//不指明Content-Length用Flush的话不会显示下载进度
Response.AddHeader("Content-Length", fileSize.ToString());
Response.TransmitFile(filePath, 0, fileSize);
Response.Flush();
Response.Close();
}
//使用WriteFile下载文件
protected void btnDL2_Click(object sender, EventArgs e)
{
FileInfo info = new FileInfo(filePath);
long fileSize = info.Length;
Response.Clear();
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachement;filename=" + fileName);
//指定文件大小
Response.AddHeader("Content-Length", fileSize.ToString());
Response.WriteFile(filePath, 0, fileSize);
Response.Flush();
Response.Close();
}