zoukankan
html css js c++ java
Asp.net(c#)实现多线程断点续传
Code
1
System.IO.Stream iStream
=
null
;
2
3
//
Buffer to read 10K bytes in chunk:
4
byte
[] buffer
=
new
Byte[
10240
];
5
6
//
Length of the file:
7
int
length;
8
9
//
Total bytes to read:
10
long
dataToRead;
11
12
//
Identify the file to download including its path.
13
string
filepath
=
@"
E:\software\SQL Server 2000 Personal Edition.ISO
"
;
14
15
//
Identify the file name.
16
string
filename
=
System.IO.Path.GetFileName(filepath);
17
18
try
19
{
20
//
Open the file.
21
iStream
=
new
System.IO.FileStream(filepath, System.IO.FileMode.Open,
22
System.IO.FileAccess.Read,System.IO.FileShare.Read);
23
Response.Clear();
24
25
//
Total bytes to read:
26
dataToRead
=
iStream.Length;
27
28
long
p
=
0
;
29
if
(Request.Headers[
"
Range
"
]
!=
null
)
30
{
31
Response.StatusCode
=
206
;
32
p
=
long
.Parse( Request.Headers[
"
Range
"
].Replace(
"
bytes=
"
,
""
).Replace(
"
-
"
,
""
));
33
}
34
if
(p
!=
0
)
35
{
36
Response.AddHeader(
"
Content-Range
"
,
"
bytes
"
+
p.ToString()
+
"
-
"
+
((
long
)(dataToRead
-
1
)).ToString()
+
"
/
"
+
dataToRead.ToString());
37
}
38
Response.AddHeader(
"
Content-Length
"
,((
long
)(dataToRead
-
p)).ToString());
39
Response.ContentType
=
"
application/octet-stream
"
;
40
Response.AddHeader(
"
Content-Disposition
"
,
"
attachment; filename=
"
+
System.Web.HttpUtility.UrlEncode(Request.ContentEncoding.GetBytes(filename)));
41
42
iStream.Position
=
p;
43
dataToRead
=
dataToRead
-
p;
44
//
Read the bytes.
45
while
(dataToRead
>
0
)
46
{
47
//
Verify that the client is connected.
48
if
(Response.IsClientConnected)
49
{
50
//
Read the data in buffer.
51
length
=
iStream.Read(buffer,
0
,
10240
);
52
53
//
Write the data to the current output stream.
54
Response.OutputStream.Write(buffer,
0
, length);
55
56
//
Flush the data to the HTML output.
57
Response.Flush();
58
59
buffer
=
new
Byte[
10240
];
60
dataToRead
=
dataToRead
-
length;
61
}
62
else
63
{
64
//
prevent infinite loop if user disconnects
65
dataToRead
=
-
1
;
66
}
67
}
68
}
69
catch
(Exception ex)
70
{
71
//
Trap the error, if any.
72
Response.Write(
"
Error :
"
+
ex.Message);
73
}
74
finally
75
{
76
if
(iStream
!=
null
)
77
{
78
//
Close the file.
79
iStream.Close();
80
}
81
Response.End();
82
}
查看全文
相关阅读:
【小工具大用处】10个超实用的设计师专属Chrome小插件
作为自由网页开发者如何建立自己的个人品牌【2019最详细教程】
你的作品集够好了吗?20份精选UI设计作品集给你灵感
【编程指南】新手上路不必忧,全球十大顶尖网站免费用!
配色指南|你知道如何正确使用红色与绿色吗?
摹客专访 | “猫系”设计师——开到茶花
25个故事性网页设计,轻松讲述网页独有的故事~
Mockplus四周年大回馈来啦!
layer 弹出层
SVN 安装vs插件
原文地址:https://www.cnblogs.com/zhangchenliang/p/981239.html
最新文章
System.ComponentModel.Win32Exception解决方案
System.ComponentModel.Win32Exception解决方案
Atom,github出品编辑器,未来最好的编辑器
git自学总结
webgis、gis学习技巧总结
arcgisapi for javascript4.0学习笔记4.0新特性
Dojo页面布局设计
AE系统开发大概流程和知识框架浅显列表
DevExpress GridControl Gridview RepositoryItemCheckEdit复选框及获取选择行数据
JQuery的each
热门文章
css背景图片位置:background的position
javascript:void到底是个什么?
offsetTop offsetLeft offsetWidth offsetHeight
元素重叠及position定位的z-index顺序
margin设置为负数
面试题
css打造自己的博客园
[转载]Div和Table的区别
【开发新手福利】—Bootstrap运用终极指南
设计技巧|一键轻松实现长页面滚动,精确定位!
Copyright © 2011-2022 走看看