日期:2014-05-17  浏览次数:20647 次

下载.doc .txt文件时自动打开怎么办
<a href="~/down/文件.rar">下载<a>

这段代码可以下载除.doc .txt这种系统不能识别的文件
问题是如果要下载的文件是.doc .txt这类文件直接就会被打开 怎么办啊

------解决方案--------------------
用这个 或者按楼上的
/// <summary>
/// 下载文件
/// </summary>
/// <param name="path">文件路径</param>
public static void DownLoad(string path)
{
System.IO.FileInfo fi = new System.IO.FileInfo(path);
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.Buffer = false;

HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(System.Text.Encoding.UTF8.GetBytes(path)));
HttpContext.Current.Response.AppendHeader("Content-Length", fi.Length.ToString());
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.WriteFile(path);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();

}