本文实例讲述了asp.net下载大文件的实现方法。分享给大家供大家参考。具体分析如下: C% S8 j0 D! y( Y- X q3 G! B' i
当我们的网站需要支持下载大文件时,如果不做控制可能会导致用户在访问下载页面时发生无响应,使得浏览器崩溃。可以参考如下代码来避免这个问题。
, L# N! A6 S: \' V9 ]0 H关于此代码的几点说明:3 x$ m$ M4 ?3 t- |# i' E# W* X
1. 将数据分成较小的部分,然后将其移动到输出流以供下载,从而获取这些数据。1 h1 u: ^7 X/ @' i; W
2. 根据下载的文件类型来指定 response.contenttype 。(参考oschina的这个网址可以找到大部分文件类型的对照表:)
& t1 N8 \- x% |2 ~3 r& t3. 在每次写完response时记得调用 response.flush()
( B5 l1 I! {: u+ n4. 在循环下载的过程中使用 response.isclientconnected 这个判断可以帮助程序尽早发现连接是否正常。若不正常,可以及早的放弃下载,以释放所占用的服务器资源。' X# |" S5 z6 t& d2 b' ~* w
5. 在下载结束后,需要调用 response.end() 来保证当前线程可以在最后被终止掉。
3 p) {0 I) `- z' T' J8 R+ B代码如下:1 d5 S; @+ g, a& g0 p
using system;0 m2 t+ O' _# A( B- |
namespace webapplication1
/ I8 Z2 a4 e: H1 a" `{/ {6 {8 s* p0 M' b! u9 b
public partial class downloadfile : system.web.ui.page4 Q0 o# m! Y7 `1 P
{( Z& E1 n6 L" y$ F
protected void page_load(object sender, eventargs e)
& o" U. T ? r: H# f* h{
/ q m/ I; }$ h# _4 B; ^system.io.stream istream = null;
# I: n4 a `1 e$ ?! Z// buffer to read 10k bytes in chunk:
6 A7 U) v& q: [! [. wbyte[] buffer = new byte[10000];3 [) N* w# S5 D
// length of the file:: C# ^ h! t( y4 n
int length;
7 z4 ?+ Q6 p* |# c: k2 j" R9 `// total bytes to read.4 D) Y- l t% O5 D! @; T5 O7 u
long datatoread;
* H o7 [( {; W. m, `+ Q// identify the file to download including its path.
$ w; p) U5 \& V5 H v( f8 nstring filepath = server.mappath(/) +./files/textfile1.txt;
& x) Y; u1 U* n// identify the file name.
1 P6 s5 q2 D5 {string filename = system.io.path.getfilename(filepath);
5 |7 {* C) F9 {! Wtry
: m, m9 [- }! T( A* v+ h( Z{
0 i* p; S' Q3 x( l4 t4 K2 S/ m// open the file.
$ w5 |+ r( w- G7 xistream = new system.io.filestream(filepath, system.io.filemode.open, V" l7 R1 D- N
system.io.fileaccess.read, system.io.fileshare.read);7 _: q: x3 c2 R, `4 q9 I7 U
// total bytes to read.. F5 J$ L7 F& a n. X
datatoread = istream.length;
5 Q! I+ |' }+ ^( ^+ \- Yresponse.clear();
! f( B/ [8 F3 o: l6 A* n# Presponse.clearheaders();* Q: l! b0 @. M( W% H1 C ~
response.clearcontent();6 C1 \/ G. V, G' [0 o# ^/ m
response.contenttype = text/plain; // set the file type- P$ e3 ?( v7 [' ~: t6 l( S) L/ R
response.addheader(content-length, datatoread.tostring());
6 W2 H4 t& e. w S/ b" |0 ]response.addheader(content-disposition, attachment; filename= + filename);
0 A" j; E" p5 C: C3 \; i( L2 V. y3 I// read the bytes.4 H% M& Y5 }9 s/ ?4 B7 s) z
while (datatoread > 0)
* i. F6 O ]: Z" w{& p" B1 ?! [, j# z6 h& G- F
// verify that the client is connected.1 C ?1 h O; y! Q7 O1 g
if (response.isclientconnected)
# ~1 H- E! J$ N8 x5 E7 l. G{$ ^, h& @9 ~* B7 X1 W; j: j
// read the data in buffer.
* P0 W0 o- Q$ ]; nlength = istream.read(buffer, 0, 10000);/ f4 y( |9 |- s+ ? J
// write the data to the current output stream.
) l9 i, o O1 F9 ]response.outputstream.write(buffer, 0, length);( r7 T2 t) j8 H
// flush the data to the html output.
" f. C/ z }4 Lresponse.flush();
$ S' V+ L# i* Xbuffer = new byte[10000];; K* |) {( Z& V
datatoread = datatoread - length;
}- H0 t( M4 }0 B( C1 z$ r}
0 r$ x# c+ ^! J: R! \6 O7 r6 felse
6 @0 ?; r# X2 l" E4 ~{
( Z H# a# l# I& w/ v1 `- l// prevent infinite loop if user disconnects9 M, h# {+ i+ j
datatoread = -1;, a+ `1 V1 V0 J- W; O6 V$ ~" f
}
9 n2 z% e- N) n* ^2 o+ l/ }}% \' h& D; l2 z9 _& p6 Q
}
& `, |8 W% G) l1 o2 b2 ycatch (exception ex)
# S% V5 j3 p& P{( ]4 h! Y$ ^/ ]- M" A: C' b# ]; e
// trap the error, if any.% Q: S$ u# H. F2 g. O% `
response.write(error : + ex.message);
/ Y# w: D# Z. E# |}
( M: C; _3 l" r2 [! R5 @5 h7 xfinally
7 W% [7 R3 N' ?9 \2 \{) b# b9 O7 d7 f# c) f! y
if (istream != null)
( u0 J, x7 X1 Q7 [, S6 O{- W: u! B* P, u+ }
//close the file.$ ]! K: u+ c# w) k6 U
istream.close();
/ T+ ^) T$ L3 {+ C% P* R}% e+ b( ^6 q$ w4 B
response.end();0 r7 t0 K) e7 c) t1 Q
}
6 o! D5 r6 a, a# h/ L$ F' G0 A}! _/ ]$ l! ~' F- q1 `6 [
}, D5 w" E4 b4 D; p$ J( I/ W
}
. ?# n% G* n0 f- s. D& ^希望本文所述对大家的asp.net程序设计有所帮助。7 O& ~: U( c: P) q
* m5 S4 K3 U: w- J+ r" T4 G
更多网页制作信息请查看: 网页制作 |
|