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
}
查看全文
相关阅读:
包含源文件 —— 是奇技淫巧还是饮鸩止渴?
感谢各位网友——《品悟C——抛弃 C程序设计 中的谬误与恶习》正式出版
能否用痰盂盛饭——谈谈在头文件中定义外部变量
C:劣书简易鉴别法
从“站在巨人的肩上”到“跪到侏儒之脚下”——图灵公司副主编自供(二)
劣质代码评析——刻舟求剑的故事
Python自然语言处理学习笔记(36): 4.8 Python库的样本
Python自然语言处理学习笔记(38): 4.10 深入阅读
Python自然语言处理学习笔记(37):4.9 小结
Python自然语言处理学习笔记(35): 4.7 算法设计
原文地址:https://www.cnblogs.com/zhangchenliang/p/981239.html
最新文章
表现层持续解耦带来的模式转变 MVC MVP MVVM
【11平台天梯】【原理分析】11平台天梯原理分析
JK_Rush关于索引的一些总结
Android 移植到 C#
Windows Phone支持数据库汇总
异步Socket服务器与客户端
shell循环目录操作,尽量使用绝对路径,相对路径会报错
reflect反射的实际中的应用及畅想
go类型安全转换
Dockerfile 优化
热门文章
go 如何让在强制转换类型时不发生内存拷贝?
go对象选择器自解引
go的命名返回值有时,函数体内直接可以等于,不需要再重新:=或者var声明
Go常量const
go 怎么让goroutine跑一半就退出?
go标准库之flag包
从“站在巨人的肩上”到“跪到侏儒之脚下”——图灵公司副主编自供(一)
【转】【随笔(或曰“扯文”)】记念我22年前的广播室Operator生涯——陈副总编的“死穴”
劣质代码评析——兼谈指针越界问题
亡羊补牢还是越错越远——“C99允许在函数中的复合语句中定义变量”
Copyright © 2011-2022 走看看