文件下载
来源:互联网 发布:淘宝双11交易数据 编辑:程序博客网 时间:2024/06/05 03:36
//引用命名空间using System.IO;using System.Text;using System.Web;// 文件下载public static bool DownloadFile(string path) { HttpRequest request = HttpContext.Current.Request; HttpResponse response = HttpContext.Current.Response; try { FileInfo info = new FileInfo(HttpContext.Current.Server.MapPath(path)); if (info.Exists) { string name = info.Name; string extension = info.Extension; name = name.Substring(0, name.Length - extension.Length); while (HttpUtility.UrlEncode(name + extension, Encoding.UTF8).Length > 0x9c) { name = name.Substring(0, name.Length - 1); } name = HttpUtility.UrlEncode(name + extension, Encoding.UTF8); response.Clear(); if (info.Length < 0xfa000L) { response.ContentType = "application/octet-stream"; response.AddHeader("Content-Disposition", "attachment;filename=" + name); response.WriteFile(info.FullName); } else { FileStream input = new FileStream(HttpContext.Current.Server.MapPath(path), FileMode.Open, FileAccess.Read, FileShare.ReadWrite); BinaryReader reader = new BinaryReader(input); try { long length = input.Length; long num2 = 0L; int count = 0x2800; if (request.Headers["Range"] != null) { response.StatusCode = 0xce; num2 = Convert.ToInt64(request.Headers["Range"].Split(new char[] { '=', '-' })[1]); } response.AddHeader("Content-Length", (length - num2).ToString()); if (num2 != 0L) { response.AddHeader("Content-Range", string.Format(" bytes {0}-{1}/{2}", num2, length - 1L, length)); } response.ContentType = "application/octet-stream"; response.AddHeader("Content-Disposition", "attachment;filename=" + name); reader.BaseStream.Seek(num2, SeekOrigin.Begin); int num4 = ((int)Math.Floor((double)(((double)(length - num2)) / ((double)count)))) + 1; for (int i = 0; i < num4; i++) { if (response.IsClientConnected) { response.BinaryWrite(reader.ReadBytes(count)); response.Flush(); } else { i = num4; } } } catch { return false; } finally { reader.Close(); input.Close(); } } } } catch (Exception) { return false; } return true; }
阅读全文
0 0
- 下载图片,下载文件
- 文件下载
- 文件下载
- 文件下载
- 文件下载
- 文件下载
- 文件下载
- 下载文件
- 文件下载
- 下载文件
- 文件下载
- 文件下载
- 下载文件
- 文件下载
- 下载文件
- 下载文件
- 文件下载
- 下载文件
- 【R语言 可视化】R语言画图增加次要刻度线
- Android5.0后的圆圈动画
- [Java Web]servlet/filter/listener/interceptor区别与联系
- linux下安装mysql总结(用N种方法还是安装版好用)
- EditText光标控制和软键盘隐藏
- 文件下载
- python第六篇之break
- c#
- 前端回车键登录-- 随意写些 -- 签到用的
- C++学习笔记12:类成员初始化等问题
- 车牌检测网络资源
- HTTP+ 加密 + 认证 + 完整性保护 =HTTPS
- unity3dC#的List升序降序排序
- linux下安装redis总结