惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

爱范儿
爱范儿
量子位
人人都是产品经理
人人都是产品经理
小众软件
小众软件
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - Franky
Recent Announcements
Recent Announcements
A
About on SuperTechFans
Microsoft Security Blog
Microsoft Security Blog
N
Netflix TechBlog - Medium
H
Help Net Security
博客园 - 三生石上(FineUI控件)
博客园 - 司徒正美
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
MyScale Blog
MyScale Blog
The Cloudflare Blog
S
SegmentFault 最新的问题
H
Hackread – Cybersecurity News, Data Breaches, AI and More
J
Java Code Geeks
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
P
Proofpoint News Feed
宝玉的分享
宝玉的分享
Vercel News
Vercel News

博客园 - Sphix

根据银行卡获取银行卡开户银行和类型 HTTP could not register URL http://+:8000/testservice/. Your... 一些必不可少的Sublime Text 2插件 C# CultureInfo列表 解决"There is already an open DataReader associated with this Command which must be closed first." exception in EF 中 C#正则表达式整理备忘 Lucene.net 开发记录 在Asp.net 4.0 中动态注册HttpModule ASP.net 视频上传转换flv并且抓取第一帧生成图片源码 揭秘全球最大网站Facebook背后的那些软件 asp.net网站管理工具 的 地址(Web Site Administration Tool ) Sql Server 2008 Full-text search Error: Word breaking timed out for the full-text query string. Associations in EF Code First CTP5: Part 1 – Complex Types C# Enum 类型的本地化 wordpress 文章缩略图功能 简单实际的方式分隔Admin 区域 EF4 中Self-track entity 错误用于单web开发中要注意的地方 C#验证文件类型 SQLite 资源汇总
Asp.net文件下载
Sphix · 2010-05-25 · via 博客园 - Sphix

多线程断点续传

 1         public bool ResponseDownload(string path) {
 2             long fileLength;
 3             long startRange = 0;
 4             int pack = 10240;//10k
 5             string filename=Path.GetFileName(path);
 6             try {
 7                 FileStream dStream = new FileStream(path, FileMode.Open, FileAccess.Read);
 8                 byte[] buffer = new byte[pack];
 9                 //BinaryReader br = new BinaryReader(dStream);
10                 try {
11                     Page.Response.Clear();
12                     Response.Buffer = false;
13                     Response.AddHeader("Accept-Ranges""bytes");
14                     
15                     fileLength = dStream.Length;
16                     long sp = 0;
17                     long ep = 0;
18                     if (Request.Headers["Range"!= null) {
19                         Response.StatusCode = 206;
20                         string[] range = Request.Headers["Range"].Split(new char[] { '=''-' });
21                         if (range.Length == 3&&!string.IsNullOrEmpty(range[2])) {
22                             ep = Convert.ToInt64(range[2]);
23                         }
24                         sp = Convert.ToInt64(range[1]);
25                         if (ep < sp) {
26                             ep = 0;
27                         }
28                     }
29                     long contLength = ep == 0 ? fileLength - sp : ep - sp + 1;
30                     
31                     if (sp != 0) {
32                         Response.AddHeader("Content-Range", String.Format("bytes {0}-{1}/{2}", sp, ep == 0 ? fileLength - 1 : ep, fileLength));
33                     }
34 
35                     Response.AddHeader("Content-Length", contLength.ToString());
36                     Response.AddHeader("Content-Disposition""attachment;filename=" + System.Web.HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8));
37                     Response.AddHeader("Connection""Keep-Alive");
38                     Response.ContentType = "application/octest-stream";
39                     Response.Flush();
40                     
41                     //calculate file size;
42                     long maxCount = ep==0?fileLength:ep+1;
43 
44                     maxCount = maxCount - sp;
45 
46                     //move file handler to range point.
47                     dStream.Seek(sp, SeekOrigin.Begin);
48 
49                     while (maxCount > 0&&Response.IsClientConnected) {
50                         int lengthRead = dStream.Read(buffer, 0, Convert.ToInt32(pack));
51                         Response.OutputStream.Write(buffer, 0, lengthRead);
52                         Response.Flush();    
53                         maxCount -= pack;
54                     }
55                     
56                 }
57                 catch (Exception e) {
58                     Response.Write("<h3>Error:</h3>" + e.Message);
59                     return false;
60                 }
61                 finally {
62                     dStream.Close();
63                 }
64 
65             }
66             catch (Exception ex) {
67                 Response.Write("<h3>Error:</h3>" + ex.Message);
68                 return false;
69             }
70             return true;
71         }