c# execl下载 兼容火狐 ie 谷歌
2017-02-10 10:12
411 查看
protected void LoadTemplet_Click(object sender, EventArgs e) { string fileName = "节假日导入模板.xls";//客户端保存的文件名 //filePath 为文件路径 string filePath = Server.MapPath(System.Configuration.ConfigurationManager.AppSettings["HolidayTemplet"].ToString());//路径 string savePath = filePath; if (System.IO.File.Exists(savePath)) { long chunkSize = 204800; //指定块大小 byte[] buffer = new byte[chunkSize]; //建立一个200K的缓冲区 long dataToRead = 0; //已读的字节数 FileStream stream = null; try { Encoding encoding; string outputFileName = null; string browser = HttpContext.Current.Request.UserAgent.ToUpper(); if (browser.Contains("MS") == true && browser.Contains("IE") == true) { outputFileName = HttpUtility.UrlEncode(fileName); encoding = System.Text.Encoding.Default; } else if (browser.Contains("FIREFOX") == true) { outputFileName = fileName; encoding = System.Text.Encoding.GetEncoding("GB2312"); } else { outputFileName = HttpUtility.UrlEncode(fileName); encoding = System.Text.Encoding.Default; } //打开文件 stream = new FileStream(savePath, FileMode.Open, FileAccess.Read, FileShare.Read); dataToRead = stream.Length; //添加Http头 HttpContext.Current.Response.Clear(); HttpContext.Current.Response.Buffer = true; HttpContext.Current.Response.ContentEncoding = encoding; HttpContext.Current.Response.ContentType = "application/octet-stream"; HttpContext.Current.Response.AddHeader("Content-Disposition", "attachement;filename=" + outputFileName); HttpContext.Current.Response.AddHeader("Content-Length", dataToRead.ToString()); while (dataToRead > 0) { if (HttpContext.Current.Response.IsClientConnected) { int length = stream.Read(buffer, 0, Convert.ToInt32(chunkSize)); HttpContext.Current.Response.OutputStream.Write(buffer, 0, length); HttpContext.Current.Response.Flush(); HttpContext.Current.Response.Clear(); dataToRead -= length; } else { dataToRead = -1; //防止client失去连接 } } } catch (Exception ex) { HttpContext.Current.Response.Write("Error:" + ex.Message); } finally { if (stream != null) stream.Close(); HttpContext.Current.Response.Close(); } } }
相关文章推荐
- ie 火狐 谷歌 浏览器下载,文件名字乱码,空格,不兼容问题解决
- 把网页变成黑白(兼容谷歌、火狐、IE等浏览器)
- java 中文乱码的解决办法!兼容谷歌,IE,FIREFOX(火狐)
- 工作笔记---jQuery在可编辑的div光标处插入内容 兼容IE,火狐,谷歌
- 在网页中插入FLV视频,经测试兼容IE、火狐、谷歌等浏览器
- Jquery 插件图片放大效果(兼容ie 火狐 谷歌 Safari)
- 返回顶部按钮_兼容IE678火狐谷歌(转)
- javasxript图片预加载兼容代码(IE7/IE8/IE9/火狐/谷歌/苹果)
- Asp.net 解决下载乱码问题,支持火狐、IE、谷歌等主流浏览器
- java下载文件中文文件名乱码问题(ie,谷歌,火狐)
- 如何让点聚WebOffice在线编辑ActiveX插件兼容火狐、谷歌、IE各式浏览器
- 站内搜索 高亮显示关键字(兼容火狐 谷歌 ie)
- (亲测兼容360、IE、谷歌、火狐等浏览器)鼠标悬停显示页面被截取字段的全部内容
- IE和谷歌(火狐)浏览器兼容获取内部样式表和外部样式表的样式
- 文件下载,在ie和谷歌中是好的,在火狐中名称显示不全,没有后缀类型显示application/octet-stream,或者内容乱码
- C++获取浏览器浏览历史(兼容谷歌、IE、火狐)
- IE、谷歌Cookie记录失败,火狐成功(IE和Firefox下的Cookie兼容问题)
- IE,谷歌,遨游,火狐兼容的万能合并单元格方法
- javascript JS 实现打印页面功能 支持多浏览器,兼容IE,火狐,谷歌