本文实例讲述了asp.net下载大文件的实现方法。分享给大家供大家参考。具体分析如下:3 L4 l# }4 o0 ]9 d, c
当我们的网站需要支持下载大文件时,如果不做控制可能会导致用户在访问下载页面时发生无响应,使得浏览器崩溃。可以参考如下代码来避免这个问题。
2 j. A4 R* W% R) G; Z% V! Z" A关于此代码的几点说明:
1 J6 _# S7 q" N& E1 i+ B1. 将数据分成较小的部分,然后将其移动到输出流以供下载,从而获取这些数据。. M% d; r* V5 |1 e
2. 根据下载的文件类型来指定 response.contenttype 。(参考oschina的这个网址可以找到大部分文件类型的对照表:)
3 o% i7 g2 ]- E! i! i3. 在每次写完response时记得调用 response.flush()1 L. v% H, j6 B4 h( b
4. 在循环下载的过程中使用 response.isclientconnected 这个判断可以帮助程序尽早发现连接是否正常。若不正常,可以及早的放弃下载,以释放所占用的服务器资源。/ @ t, ^3 i0 L/ {
5. 在下载结束后,需要调用 response.end() 来保证当前线程可以在最后被终止掉。
$ N. U9 K5 `2 l; m3 b代码如下:
4 I! [4 T! y4 v" e# dusing system;
3 H9 ^/ O* D4 u" \- |namespace webapplication1
' T% R' t9 L+ }# v{
/ _; a9 b% Y# Y% F% Z0 w. s7 Ipublic partial class downloadfile : system.web.ui.page! H2 O6 u) [9 k
{
1 ^8 s# `# P6 B1 Bprotected void page_load(object sender, eventargs e)
& g' W3 a. M; [5 N; e0 h9 q{( l' ~! y; {' V2 e
system.io.stream istream = null;
( d4 [. c+ |. g6 j# X+ m( O// buffer to read 10k bytes in chunk:
, `8 j" m3 K7 F$ Z$ {9 ~8 Kbyte[] buffer = new byte[10000];
, h: [8 L7 S" v y8 l7 c: _3 V' Q// length of the file:8 j5 n! m. y% G+ ?; ^
int length;
4 t( M, R& D9 T; f9 T% ?// total bytes to read.
) P. \! D" o, l! C7 l. d" U1 blong datatoread;
7 v5 t: _8 t- _8 b# y- f; f" _: L// identify the file to download including its path.
, h: _7 M7 R+ U$ I2 z/ s3 Rstring filepath = server.mappath(/) +./files/textfile1.txt; ?# Y8 q* I8 S' M' B5 |
// identify the file name.5 ~/ S# S n# s/ q$ k/ F
string filename = system.io.path.getfilename(filepath);& a. T4 w/ r- C- @# f
try, L/ @$ i+ K& K# t. W% ]
{
. J) C8 [& ^; O+ {1 Q+ U3 A; Z// open the file.
; z+ W* R) {+ b% ?7 Iistream = new system.io.filestream(filepath, system.io.filemode.open,/ T! M0 G( m7 f
system.io.fileaccess.read, system.io.fileshare.read); |2 \& s# `% R, `, Y6 w
// total bytes to read.! ~% g0 }) L& _9 o3 V/ u8 J
datatoread = istream.length;! t5 c9 d9 F2 P* s2 n: g
response.clear();
) P2 X Q8 R$ K( c, l$ W }response.clearheaders();
2 ]5 @& x- f% ]5 N7 O) fresponse.clearcontent();: p& p9 ?7 o0 K- ^# O' ^7 k
response.contenttype = text/plain; // set the file type$ D, ^& t+ _8 H$ C
response.addheader(content-length, datatoread.tostring());
7 |# U* o* i R9 T: ~response.addheader(content-disposition, attachment; filename= + filename);
7 N3 ^1 o* a% r+ O2 D, T5 j// read the bytes.) G3 `, U0 u3 u. o
while (datatoread > 0)) {( @$ Q9 j7 d Z
{' a, K e: d0 i& P; O
// verify that the client is connected.1 e# D8 P+ v+ o8 X" j
if (response.isclientconnected)3 |+ g* N+ g& H1 d7 N$ d
{
/ f+ S1 `. i& z) G" y// read the data in buffer.5 J1 U6 x9 ~8 P/ f( B
length = istream.read(buffer, 0, 10000);
& ?% E' n2 J/ n# V5 o% |3 ]% _// write the data to the current output stream.
/ B% b9 s. J, i! Kresponse.outputstream.write(buffer, 0, length);
+ }. Q8 t1 P1 C1 T5 r' Q; l* j// flush the data to the html output.% ~% U; A7 v; y( `
response.flush();4 L9 p: m; \+ E+ s5 ^
buffer = new byte[10000];
! ^0 { ^3 H) l" qdatatoread = datatoread - length;, J# Z% h/ j2 Q4 z# r0 I
}
0 O% ], y+ P) w/ s5 u8 {5 L' Lelse
; S+ t# k; T# m- c* H A{) _$ J. A- | w) O: Y0 T# ?
// prevent infinite loop if user disconnects
2 l7 Q0 j; Y; L3 [5 U; `9 J& e9 @: Qdatatoread = -1;
0 r9 n! _4 q* \}
- b' H0 e0 }% u2 x. W}; t4 C' G6 B! J! J
} r- l3 _2 [! a7 ~
catch (exception ex)
~) y& ~/ E8 N% l{0 s7 p2 C8 y9 m: o, }/ t
// trap the error, if any.; f6 d; p( a* ~8 e7 H3 |2 X
response.write(error : + ex.message);! y# z' p! C7 t9 z" H
}, e8 u) T' v# d1 R( }( v8 S7 q
finally
$ T) [- l4 k8 S+ y{
8 {- Y) |: `0 G$ U- e! lif (istream != null)" R' L+ A U7 I8 @& q
{
. a% U4 Z- n% ]2 N' G//close the file.
. f$ m. {2 g! C, pistream.close();; k- l) ?/ V9 Y8 K3 c4 a
}& k4 I' A: Q* h7 Q
response.end();# l y9 r; _* ^6 g7 ?; B" r
}- B' x& |% W8 u* x$ p O1 P# P* x# t
}
( t4 a2 w. P$ c$ h( P}
# X5 J+ s8 a3 _& U9 x4 x0 x}
/ F/ p8 J2 R# z/ S% |希望本文所述对大家的asp.net程序设计有所帮助。
0 C2 Z7 z: C7 a8 z$ u; Z ]
9 `; c H$ i' ?3 d0 }! b更多网页制作信息请查看: 网页制作 |
|