之前老版本系统上的文件流输出代码如下:
string url = PUrl(); //PUrl返回文件URL地址
string path = Server.MapPath(url);
System.IO.FileInfo file = new System.IO.FileInfo(path);
Response.Clear();
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF8;
// 添加头信息,为"文件下载/另存为"对话框指定默认文件名
string[] st = url.Split('.');
Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(DateTime.Now.ToString()+"."+st[st.Length-1]));
// 添加头信息,指定文件大小,让浏览器能够显示下载进度
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = "application/octet-stream";
// 把文件流发送到客户端
Response.WriteFile(file.FullName);
// 停止页面的执行
Response.End();
出现问题:部分用户安装了迅雷,使用IE浏览器下载时点击后弹出迅雷下载窗口却无法下载。
解决办法:改为字节流输出。
string url = PUrl();
string path = Server.MapPath(url);
System.IO.FileStream file = new System.IO.FileStream(path,System.IO.FileMode.Open);
byte[] buffer = new byte[file.Length];
file.Read(buffer,0,buffer.Length);
file.Close();
Response.Clear();
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF8;
// 添加头信息,为"文件下载/另存为"对话框指定默认文件名
string[] st = url.Split('.');
Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(DateTime.Now.ToString()+"."+st[st.Length-1]));
// 添加头信息,指定文件大小,让浏览器能够显示下载进度
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = "application/octet-stream";
// 把文件流发送到客户端
Response.BinaryWrite(buffer);
// 停止页面的执行
Response.End();
希望能对您有所帮助^_^