
























#region -- 下载文件按钮事件-----------------string FullFileName =Server.Mappath("~/"+"要下载的文件相对路径")
string strFileName = FullFileName.Substring(FullFileName.LastIndexOf("/")+1); //获取文件名(不包含路径)
FileInfo DownloadFile;
try
{
DownloadFile = new FileInfo(FullFileName); //设置要下载的文件
Response.Clear(); //清除缓冲区流中的所有内容输出
Response.ClearHeaders(); //清除缓冲区流中的所有头,不知道为什么,不写这句会显示错误页面
Response.Buffer = false; //设置缓冲输出为false//设置输出流的 HTTP MIME 类型为application/octet-stream
Response.ContentType = "application/octet-stream";//将 HTTP 头添加到输出流
Response.AppendHeader("Content-Disposition","attachment;filename=" + strFileName + DownloadFile.Extension );
Response.AppendHeader(
"Content-Length", DownloadFile.Length.ToString());//将指定的文件直接写入 HTTP 内容输出流。//一定要注意是WriteFile不是Write(害得我搞了一晚上)Response.Flush();
//向客户端发送当前所有缓冲的输出此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。