本文实例讲述了asp.net下载大文件的实现方法。分享给大家供大家参考。具体分析如下:* ^: _. @8 X. H0 x/ K; r) @
当我们的网站需要支持下载大文件时,如果不做控制可能会导致用户在访问下载页面时发生无响应,使得浏览器崩溃。可以参考如下代码来避免这个问题。1 F! A9 W: _$ \0 B8 B1 W
关于此代码的几点说明:" B6 [2 g7 s/ B3 _# A
1. 将数据分成较小的部分,然后将其移动到输出流以供下载,从而获取这些数据。
# o9 K4 z! }3 |( v0 l7 d2. 根据下载的文件类型来指定 response.contenttype 。(参考oschina的这个网址可以找到大部分文件类型的对照表:)
9 q2 T( K/ X, P6 |* L3 F3. 在每次写完response时记得调用 response.flush()* ]3 M- g9 W2 q5 g
4. 在循环下载的过程中使用 response.isclientconnected 这个判断可以帮助程序尽早发现连接是否正常。若不正常,可以及早的放弃下载,以释放所占用的服务器资源。
- ]( s1 [5 m, s6 c& b& B5. 在下载结束后,需要调用 response.end() 来保证当前线程可以在最后被终止掉。- X9 T, N+ e3 H
代码如下:1 a6 k8 q! B: J! i/ |
using system;/ q& e) p! o- N; Y( L& X+ T- r
namespace webapplication1& l( v) n3 K& i7 w: V
{
+ f2 t9 z9 y; y6 b: o( ]public partial class downloadfile : system.web.ui.page0 X4 Z' J& h9 o. u; [! d) r- e
{
( w- S6 e+ b9 Iprotected void page_load(object sender, eventargs e)) x$ C8 ]' I; i: E. a
{8 S R4 s3 j" G/ x; y0 T/ y
system.io.stream istream = null;8 Z- y% a) c* M: N8 U( A
// buffer to read 10k bytes in chunk:
& }6 U0 J$ n+ ]# _5 z1 y. Abyte[] buffer = new byte[10000]; T8 E7 \9 `/ i
// length of the file:$ @3 _ W# D8 f3 {8 D8 m: x& m4 s
int length;
4 i3 [' m% F; {, j" u( ^* z// total bytes to read.; X6 b, j' P# t0 r6 @0 R
long datatoread;2 \# {4 t5 }$ w6 a5 _) O9 \5 L
// identify the file to download including its path.& P6 w, r9 E7 W0 \1 u
string filepath = server.mappath(/) +./files/textfile1.txt;
. P! M1 w. B' n2 e/ ^7 J" Y// identify the file name.# ]( i* t: S8 b) n8 v
string filename = system.io.path.getfilename(filepath);
5 | E; R8 f/ {: ?" A5 W) S; f0 qtry2 V8 q6 {) f/ G: v2 A% x* `6 E" ?
{
" ] v" O: q c+ _0 c* @& f+ v// open the file., _* v* u* R- o; ?# [" v
istream = new system.io.filestream(filepath, system.io.filemode.open,
# r5 V7 n2 n/ G" c K& |. [& ^" asystem.io.fileaccess.read, system.io.fileshare.read);
& [- F9 w+ O% [4 n" C3 ?! c( M// total bytes to read.
% C7 J* S4 H$ |datatoread = istream.length;
; q7 P( ^8 i: Presponse.clear();5 M+ f6 j7 j( W* K( V$ j
response.clearheaders();$ e% d: {: e0 o2 e% N1 n. `
response.clearcontent();
/ y, Z% m( N+ q: E- a5 @response.contenttype = text/plain; // set the file type
3 R: a+ R( ^8 u7 b, s( W" s/ Kresponse.addheader(content-length, datatoread.tostring());
& l0 X% R0 P0 I) a$ kresponse.addheader(content-disposition, attachment; filename= + filename);
v+ r% j u# E! U" Q/ W/ {// read the bytes.) u7 U, @0 g* D+ e$ K
while (datatoread > 0)
, `8 E1 H. L' \7 s1 h{
8 _0 m! U. p' b7 ]% U ]// verify that the client is connected.
2 J5 }3 v& W( B* `: d+ |if (response.isclientconnected): Q4 U5 R3 S2 @+ U1 `
{1 c7 ?$ s$ {7 R# f% ^: `
// read the data in buffer.5 V7 d& d; p( u& k
length = istream.read(buffer, 0, 10000);. B- w/ E/ b% m2 e8 L
// write the data to the current output stream.
8 n1 `0 `- v! d. X! jresponse.outputstream.write(buffer, 0, length);
- g1 z: U& s5 p/ J$ g8 z6 I' M// flush the data to the html output.( e6 ]$ v* ] h2 x/ A
response.flush();8 w! A+ D. V9 v9 x8 n+ `* v
buffer = new byte[10000];; ^( J" j* s9 p* A
datatoread = datatoread - length;4 n, {+ O' z5 m8 O6 J2 V: O
}
/ l7 t# A, i/ R e5 I, e I4 @6 aelse
: S- u8 t' V% p1 g{
|5 L, O0 O' d0 L; w1 o& K// prevent infinite loop if user disconnects
: W" L4 H1 k1 h4 F* v0 udatatoread = -1;
6 D# ^4 g/ I- Q) \9 G% @! d, F9 ]$ z}
! f( U( Q8 m! D5 i}; {' L8 Y# n' W- R
}
9 `* V; V& F; W6 i) e* X9 X- X6 ]8 Lcatch (exception ex)1 J' x! {+ x+ |" v. d
{9 r% n% o2 @* H8 }3 m% [
// trap the error, if any.& O; P% h) y& R% x; {
response.write(error : + ex.message);( O5 Y( F% P) X7 J: @6 p& L
}
0 B- q6 b5 p$ q9 wfinally2 F0 b2 C" P! G7 Q7 ?8 x
{0 }. B; M/ {9 {) B0 z
if (istream != null)
! j( U. u7 A! d r, u' ]{
o) ]4 V- f o+ I//close the file.# I0 F- z; _2 i r4 p% u6 E3 D9 E/ V
istream.close();) u. Y+ L: ~( f9 p5 Z J1 c: o. i
}
+ _/ I# Z6 p$ E2 o; ^+ @* Y6 w; B* fresponse.end();
5 f& I, ?, _; g) y+ Q1 D}
( p- O8 X7 U1 y( ?# r}
6 y. ~ _" u Q; z% ~) g8 M& N4 R}
5 q2 b1 \2 T% K6 p3 F$ n2 H# S}
8 ~: q+ P/ d% f: P' [" D* m希望本文所述对大家的asp.net程序设计有所帮助。
( H, N5 E3 |# E% }7 g$ R; F3 j1 [; P- a8 E
更多网页制作信息请查看: 网页制作 |
|