得知互动
标题:
asp.net下载大文件的实现方法
[打印本页]
作者:
swmozowtfl
时间:
2015-7-31 22:04
标题:
asp.net下载大文件的实现方法
本文实例讲述了asp.net下载大文件的实现方法。分享给大家供大家参考。具体分析如下:
' S% I6 N( _9 e0 `. S1 P3 Y
当我们的网站需要支持下载大文件时,如果不做控制可能会导致用户在访问下载页面时发生无响应,使得浏览器崩溃。可以参考如下代码来避免这个问题。
7 H( L. e. ?, g) o. u
关于此代码的几点说明:
9 d+ }# J* t9 i& F
1. 将数据分成较小的部分,然后将其移动到输出流以供下载,从而获取这些数据。
0 v. H0 S3 a7 M6 s1 ~
2. 根据下载的文件类型来指定 response.contenttype 。(参考oschina的这个网址可以找到大部分文件类型的对照表:)
- t8 }1 }( ?. E
3. 在每次写完response时记得调用 response.flush()
; Y0 J6 P% c- w2 M& X9 o
4. 在循环下载的过程中使用 response.isclientconnected 这个判断可以帮助程序尽早发现连接是否正常。若不正常,可以及早的放弃下载,以释放所占用的服务器资源。
4 M: T9 {0 F7 P S |
5. 在下载结束后,需要调用 response.end() 来保证当前线程可以在最后被终止掉。
) C" `: Z& z- i+ r" Z$ J$ k" d8 \+ k
代码如下:
/ q) p7 ~7 R( X6 I" P; ~
using system;
6 g* p( V& K0 o/ f. f; T
namespace webapplication1
+ j% ]. w, @. g/ J; R+ t: d
{
! n* V# ~ w D9 K J/ B& v
public partial class downloadfile : system.web.ui.page
4 `" T, j# b: a! K$ c. j9 d
{
/ w* {6 l' `' G% U; w& s* G
protected void page_load(object sender, eventargs e)
. W8 p* T- I v) q9 F/ o
{
, f9 M o) j$ M! g2 S% o
system.io.stream istream = null;
3 l0 d% s( H- L% X
// buffer to read 10k bytes in chunk:
- {: y0 A F3 z& ^
byte[] buffer = new byte[10000];
; S( [7 r: U3 Q
// length of the file:
/ w8 {' S, _: M7 R. w
int length;
$ S* z1 ], Z7 n3 v/ ^: n
// total bytes to read.
& r! k: L. |6 D0 I% F( Z
long datatoread;
1 f. V) D2 d5 p4 A* S* f! ~9 n2 P! j
// identify the file to download including its path.
: A3 l( F8 v& K
string filepath = server.mappath(/) +./files/textfile1.txt;
2 T, B. G+ V2 r [2 R" \
// identify the file name.
* i' l& q, E9 e4 i) |; \+ b
string filename = system.io.path.getfilename(filepath);
4 u8 E! A% Z8 g
try
. f# [5 x% H! C) B8 h+ P8 K3 [9 g
{
9 y7 v, i, A; N: A# d
// open the file.
* g$ ~# G2 n* ]& ]+ O3 r7 O) t, G7 W
istream = new system.io.filestream(filepath, system.io.filemode.open,
7 U, B* t f( d
system.io.fileaccess.read, system.io.fileshare.read);
( n# N: O/ l' v* |) l
// total bytes to read.
8 q* n+ m3 U3 Q% ?9 ~1 V
datatoread = istream.length;
5 _. @5 K$ P9 C' ?
response.clear();
# T6 p2 ~# E9 m( z3 w
response.clearheaders();
' N) q9 ?* }) E: a9 |
response.clearcontent();
$ _! k6 B# @- p) O
response.contenttype = text/plain; // set the file type
2 V: [% J* L' x$ j9 D5 v U" c
response.addheader(content-length, datatoread.tostring());
9 v1 G" L, D* Q( O8 C# w
response.addheader(content-disposition, attachment; filename= + filename);
- a4 J @- E) i! J( Q9 Y
// read the bytes.
! R: Y) G# K( O; o* K
while (datatoread > 0)
$ [% u0 K+ B A& U& U# g- j. z3 g
{
* m4 R+ n/ _( [+ y$ L+ c
// verify that the client is connected.
/ k) e _8 J C4 i8 j& C1 P! z! S/ |
if (response.isclientconnected)
$ U, x- X( ?" v
{
- t& c/ h& p" a6 m. x: x
// read the data in buffer.
/ w; T1 A+ O7 R
length = istream.read(buffer, 0, 10000);
, J7 _0 w& X, N8 X, K+ m
// write the data to the current output stream.
* D8 ~; N! Y3 H# [: ^
response.outputstream.write(buffer, 0, length);
- g! z& z/ r6 w( Y" Y5 ]0 A
// flush the data to the html output.
3 r. s* Z; p7 P& }4 T6 ~
response.flush();
6 V; b1 o$ h3 u
buffer = new byte[10000];
, O0 h1 W- m- s1 u; J: h4 x& ~) e
datatoread = datatoread - length;
, K8 x0 q3 C2 A9 K0 }$ M
}
! k( s5 k0 @3 h7 b: Q
else
; x( }+ E1 ?7 X8 S
{
) \7 \& U, D1 o$ t2 l; r+ n
// prevent infinite loop if user disconnects
4 d$ p) W+ u7 F% h# `: q
datatoread = -1;
# W; @9 Y# g" z1 Q
}
0 f: z! D0 r6 U. o1 f+ j& D
}
7 G. z$ C( r6 b$ ?6 F
}
- o& }4 E& P: g+ B) _ f/ O
catch (exception ex)
& x3 k* c Z; ]( s N2 h
{
, o/ @# Q1 i% g5 `0 ~& w3 l, Q; X
// trap the error, if any.
3 f+ \* L# m5 [" E8 w P& f
response.write(error : + ex.message);
" l$ o0 o) _& B7 U/ c
}
+ ?0 a9 V, B/ t1 x$ q& V4 n. Z
finally
4 c! T- K5 q4 D5 ?
{
5 T. D; i" ?) b2 v5 d: N
if (istream != null)
. P/ J5 @/ H$ ?5 {) V4 ~
{
$ B3 y! n- x% {' @9 H: n' w! n
//close the file.
& B! k3 k4 R( h3 W& l5 Y8 W
istream.close();
& L* a) B- ~1 p3 B4 S
}
0 s$ ^* j- P) {) K6 t
response.end();
5 @5 V" a1 n8 n& q! `
}
) c* k2 ?* V' C3 u6 a) C
}
& Y5 B; h4 d+ | a. ~- Y6 y
}
1 |( R+ Q# o, D8 U3 W! R$ X, H
}
- b' ]0 h- U3 S9 O" r$ O' M* P: m
希望本文所述对大家的asp.net程序设计有所帮助。
1 }2 c4 ]2 y; l7 t# h
1 r0 N9 ]. G8 v6 B& y/ T8 E
更多网页制作信息请查看: 网页制作
作者:
seazvyt
时间:
2015-9-13 18:22
对于这种刚发的帖子,我总是毫不犹豫的顶了。如果火了就是个前排,可以混个脸熟,说不定谁好心就给粉了…稳赚不赔;如果沉了就感觉是我弄沉的,很有成就感,还能捞经验。
作者:
mwxny
时间:
2015-9-13 18:22
这篇帖子构思新颖,题材独具匠心,段落清晰,情节诡异,跌宕起伏,主线分明,引人入胜,平淡中显示出不凡的文学功底,可谓是字字珠玑,句句经典,是我辈应当学习之典范(不好意思回错帖了
作者:
Mqokjdvq
时间:
2015-9-13 18:23
感觉这个论坛的站长太牛B了,好强大啊
作者:
buingeEvineus
时间:
2015-9-13 18:23
这个论坛值得推荐,给了我们这么好的一个平台
作者:
wwzcdenleclv
时间:
2015-9-13 18:23
这就是我斗胆的一点粗略分析,每天睡觉以前,我都会把您的帖子再三拜读,拜读。
作者:
seazvyt
时间:
2015-12-25 20:48
老大,我好崇拜你哟
作者:
mwxny
时间:
2015-12-25 20:48
这个论坛值得推荐,给了我们这么好的一个平台
作者:
effoggikeftor
时间:
2015-12-25 20:48
只是本着“看贴(虽然看不懂)回贴,利人利己的中华民族优秀传统美德”,顺便赚点积分。
作者:
effoggikeftor
时间:
2015-12-25 20:49
cd:遮~~~~~~
欢迎光临 得知互动 (https://bbs.dezhifl.com/)
Powered by Discuz! X5.0