本文实例讲述了asp.net下载大文件的实现方法。分享给大家供大家参考。具体分析如下:6 ?! _8 Y3 w( J0 h" ^* ]
当我们的网站需要支持下载大文件时,如果不做控制可能会导致用户在访问下载页面时发生无响应,使得浏览器崩溃。可以参考如下代码来避免这个问题。$ ^7 p& {) }) j; w0 e- V5 O
关于此代码的几点说明:5 d- ^8 H! t1 T, S( h! O1 M
1. 将数据分成较小的部分,然后将其移动到输出流以供下载,从而获取这些数据。* v# [0 Y8 t' U# R1 I) ~
2. 根据下载的文件类型来指定 response.contenttype 。(参考oschina的这个网址可以找到大部分文件类型的对照表:)
/ v& K8 w* R& M2 A% ]0 B3. 在每次写完response时记得调用 response.flush()
$ O* O) u3 ]/ z% g: l8 o4. 在循环下载的过程中使用 response.isclientconnected 这个判断可以帮助程序尽早发现连接是否正常。若不正常,可以及早的放弃下载,以释放所占用的服务器资源。# C% O9 e- O% N
5. 在下载结束后,需要调用 response.end() 来保证当前线程可以在最后被终止掉。, d6 A0 g; O" `" j! K- q" e# c" n' m
代码如下:
) ~5 }5 b" N* i% t0 ?using system;! }8 y P. C0 x* h
namespace webapplication1
! n( S5 Z( _2 ~2 P5 j5 X{8 z8 y: c" p7 ?6 s1 g5 @0 z$ e
public partial class downloadfile : system.web.ui.page% ~, E% u: y* D- C- G9 b. p
{4 @3 E) ?( t9 G( m! ^
protected void page_load(object sender, eventargs e)5 l! K" Z2 R* W9 R& b
{5 p1 h4 g- H: R7 K2 N
system.io.stream istream = null;3 R2 A3 W( f. g; D |) u+ X. V J
// buffer to read 10k bytes in chunk:. M" s& a" f- q a) n+ p/ U/ M
byte[] buffer = new byte[10000];$ _5 l2 o) B1 }# `+ A5 }8 F$ F
// length of the file:
7 U3 N z* _6 N+ c% |, {. aint length;7 v0 m9 E6 W- K) ?- Z
// total bytes to read.
# R/ s [% i- y$ N) z% u" E9 Z% Elong datatoread;3 N- Y0 w; o' y
// identify the file to download including its path. b& }) X) t2 j4 L6 @
string filepath = server.mappath(/) +./files/textfile1.txt;
( a2 x0 k# {! N, P+ X: U4 G// identify the file name.
( z; u( k) z: R5 B0 `. s& Kstring filename = system.io.path.getfilename(filepath);
* B, w. H7 |" Q; v" x9 Mtry# }; K1 e1 r8 e( y X `/ v
{ J3 L5 { R' _ }0 Q! Q' H# J
// open the file.
( [; z. h5 _+ `. \3 Qistream = new system.io.filestream(filepath, system.io.filemode.open,
- Z+ K! n6 p5 F0 i( t; Bsystem.io.fileaccess.read, system.io.fileshare.read);. R- @; g$ I5 J& b7 ~
// total bytes to read.
1 i- g# Q0 h/ R7 _" u( g. qdatatoread = istream.length;$ w0 S; k S- g+ h/ ?0 p
response.clear();
; B5 u5 w. u, H1 ^0 G. f4 yresponse.clearheaders();
1 O) i% ]* j3 z1 Yresponse.clearcontent();
# _1 z8 \* ^# j+ E! N8 gresponse.contenttype = text/plain; // set the file type* @3 O& D6 t) J) X# M% J
response.addheader(content-length, datatoread.tostring());
! H& s8 n. m& _8 S8 g8 t7 Mresponse.addheader(content-disposition, attachment; filename= + filename);
7 ^, E' i& b) w# j3 @/ C// read the bytes.) }# u" }( t I& Z8 x% I& i
while (datatoread > 0)2 q" B9 G9 E3 E9 k! P. s' b4 ]
{$ Q6 ` \) E/ L; g' `
// verify that the client is connected.! r) f6 \! W2 c+ _, u5 i
if (response.isclientconnected)( ]4 c9 t# n& D- @6 ^ Y# s" |( `
{
8 S7 o2 [! x4 Q* r5 k. G/ F// read the data in buffer.
3 K) I+ X x2 b+ V' {: B0 mlength = istream.read(buffer, 0, 10000);
2 E! n4 K @4 I3 b4 P// write the data to the current output stream. B+ i N, F+ p" o6 {. J& U
response.outputstream.write(buffer, 0, length);5 T( |1 S9 b8 f* I. K
// flush the data to the html output.# b/ c- L* r1 d# Q
response.flush();
2 u) Y! h4 v' A& {# |0 Hbuffer = new byte[10000];: b/ w: J4 w4 @% d' q5 Y7 H9 t
datatoread = datatoread - length;4 z5 }. S+ w6 k5 W
}6 B% t' b1 }; G
else
/ c7 j/ S1 V* f8 d1 w* w: I7 M{. o2 D+ Y, E, t% T5 C
// prevent infinite loop if user disconnects8 c3 P& G) s, K) z$ H- f
datatoread = -1;
6 m/ O% h& i2 M. \' m}$ j+ D; v' d% w4 N7 l% F( q
}
2 _: i) a5 ?5 H}
. N7 s, ~; o# ]& k/ l" O$ u, ucatch (exception ex)
+ K- ^4 p1 X1 }- M7 P{
- U/ i- T p& i. h// trap the error, if any." v7 v' O, T' t5 a) T- m% ^2 w4 L
response.write(error : + ex.message);
0 a! l* b* m/ l/ I! Y( V( T}/ B b. p+ j# {" ]
finally' o" j z, K: W! w# b w! v
{+ v+ b3 ]' z9 }3 y& V0 a
if (istream != null)- X5 ], l! h# h4 `
{/ }( W; q1 k; R% ]4 t5 ]( I6 n
//close the file.
# J9 }$ f- p' W; i: l$ U _istream.close();# M; t2 h6 v/ i
}( s( E/ R3 a3 n" R
response.end();
. u% F, r! |, \8 l: _" b}* `: k. _* l* f3 y1 s6 b2 r3 r
}
4 ~; D4 X8 D6 N1 ?& ?}
3 K2 ~* U6 v: m; k+ `6 E6 F}
5 M. |- d) y7 A1 }3 v希望本文所述对大家的asp.net程序设计有所帮助。
, x7 q0 t& V: Q9 z, H9 i6 R) B+ P# i( U2 h' D7 y; o
更多网页制作信息请查看: 网页制作 |
|