在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
1 HttpResponse response = HttpContext.Current.Response; 但是有个问题了,就是会被广告拦截软件直接拦截掉,另我非常的头痛,于是寻找更好的解决方法.看了用Response.BinaryWrite写文件流一文之后觉得确实可以如此,修改代码如下:
2 string js = "<script language=javascript>window.open('{0}');</script>"; 3 js = string.Format(js, url); 4 response.Write(js); 5 1/**//// <summary>
2 /// 下载文件 3 /// </summary> 4 /// <param name="filename">文件物理地址</param> 5 6protected void DownloadFile(string filename) 7 { 8 string saveFileName = "test.xls"; 9 int intStart = filename.LastIndexOf("\\")+1; 10 saveFileName = filename.Substring(intStart,filename.Length-intStart); 11 FileStream MyFileStream; 12 long FileSize; 13 14 MyFileStream = new FileStream(filename,FileMode.Open); 15 FileSize = MyFileStream.Length; 16 17 byte[] Buffer = new byte[(int)FileSize]; 18 MyFileStream.Read(Buffer, 0, (int)FileSize); 19 MyFileStream.Close(); 20 21 Response.AddHeader("Content-Disposition", "attachment;filename="+saveFileName); 22 Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312"); 23 Response.ContentType = "application/vnd.ms-excel"; 24 25 Response.BinaryWrite(Buffer); 26 Response.Flush(); 27 Response.Close(); 28 Response.End(); 29 30 } 31
1 /**//// <summary>
2 /// 下载文件 3 /// </summary> 4 /// <param name="filename">文件物理地址</param> 5 protected void DownloadFile(string filename) 6 { 7 string saveFileName = "test.xls"; 8 int intStart = filename.LastIndexOf("\\")+1; 9 saveFileName = filename.Substring(intStart,filename.Length-intStart); 10 11 Response.Clear(); 12 Response.Charset = "utf-8"; 13 Response.Buffer= true; 14 this.EnableViewState = false; 15 Response.ContentEncoding = System.Text.Encoding.UTF8; 16 17 Response.AppendHeader("Content-Disposition","attachment;filename=" + saveFileName); 18 Response.WriteFile(filename); 19 Response.Flush(); 20 Response.Close(); 21 22 Response.End(); 23 }
使用昨天Asp.net直接保存文件到客户端 中的方法,经过我的反复测试,各式文档都运行完全正常。于是昨晚修改了现有代码,修改了下载方法,以解决一直困扰自己的窗口拦截问题。 我的操作图 各位应该看出不同之处了吧?还看不出来? 解决方法:使用lovecherry 的如何从注册表读取文件的ContentType 一文的方法 1 /**//// <summary>
2 /// 下载文件 3 /// </summary> 4 /// <param name="filename">文件物理地址</param> 5 protected void DownloadFile(string filename) 6 { 7 8 string saveFileName = "test.xls"; 9 int intStart = filename.LastIndexOf("\\")+1; 10 saveFileName = filename.Substring(intStart,filename.Length-intStart); 11 12 System.IO.FileInfo fi=new System.IO.FileInfo(filename); 13 string fileextname=fi.Extension; 14 string DEFAULT_CONTENT_TYPE = "application/unknown"; 15 RegistryKey regkey,fileextkey; 16 string filecontenttype; 17 try 18 { 19 regkey=Registry.ClassesRoot; 20 fileextkey=regkey.OpenSubKey(fileextname); 21 filecontenttype=fileextkey.GetValue("Content Type",DEFAULT_CONTENT_TYPE).ToString(); 22 } 23 catch 24 { 25 filecontenttype=DEFAULT_CONTENT_TYPE; 26 } 27 28 29 Response.Clear(); 30 Response.Charset = "utf-8"; 31 Response.Buffer= true; 32 this.EnableViewState = false; 33 Response.ContentEncoding = System.Text.Encoding.UTF8; 34 35 Response.AppendHeader("Content-Disposition","attachment;filename=" + saveFileName); 36 Response.ContentType=filecontenttype; 37 38 Response.WriteFile(filename); 39 Response.Flush(); 40 Response.Close(); 41 42 Response.End(); 43 } 44 最后得出结论:要实现柚子Nan提出的能否不考虑文件的类型,直接把文件显示到浏览器(Response),有一种方法,让客户端都不要隐藏已知的扩展名,但是这种方法是无法适应大部分电脑使用者的(一般只有比较熟悉电脑的人才会这样做吧?) |
请发表评论