Little and Mickle

All things are difficult before they are easy.

导航

Response.WriteFile()下载文件,打开对话框出现两次

以如下的代码下载文件到客户端,客户点"打开",需要点两次
     HttpContext.Current.Response.Clear();

     //     //当要下载的文件名是中文时,需加上HttpUtility.UrlEncode
     //     HttpContext.Current.Response.ClearHeaders() ;
     //     HttpContext.Current.Response.ClearContent() ;
     HttpContext.Current.Response.AddHeader("Content-Disposition","attachment;filename=" + HttpUtility.UrlEncode(fi.Name));
     HttpContext.Current.Response.AddHeader("Content-Length",fi.Length.ToString());
     HttpContext.Current.Response.ContentType="application/octet-stream";
     HttpContext.Current.Response.WriteFile(fi.FullName,0,fi.Length);
     HttpContext.Current.Response.Flush();

解决方法,客户端设置文件类型:
1.打开我的电脑
2.工具=>文件夹选项
3.文件类型
4.选中一个文件类型,点"高级",取消勾选"下载后确认打开"
这样对于选定的文件类型,就不会打开两次了。

reference:
http://support.microsoft.com/default.aspx?scid=kb;en-us;303475

posted on 2005-09-24 14:59  davidullua  阅读(1581)  评论(7编辑  收藏  举报