需要命名间:
using System.IO;
try
{
FileInfo DownloadFile = new FileInfo(@"D:/KN_Site/KN_Site/exercise.doc");
if (DownloadFile.Exists)
{
Response.Clear();
Response.ClearHeaders();
Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");//防止乱码
Response.Buffer = false;
Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode("exercise.doc", System.Text.Encoding.UTF8));
Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());
Response.WriteFile(DownloadFile.FullName);
Response.Flush();
Response.End();
}
else
{
//文件不存在
}
}
catch
{
//文件不存在
}