本文实例讲述了asp.net下载大文件的实现方法。分享给大家供大家参考。具体分析如下:& i" U1 b/ ]; U: x7 R+ i$ A6 z
当我们的网站需要支持下载大文件时,如果不做控制可能会导致用户在访问下载页面时发生无响应,使得浏览器崩溃。可以参考如下代码来避免这个问题。! d8 Z6 I, O3 k$ w
关于此代码的几点说明:
- E0 k$ t( a, l" @: N8 `: h1. 将数据分成较小的部分,然后将其移动到输出流以供下载,从而获取这些数据。3 ?$ i- l- P9 j0 y2 e! U* S
2. 根据下载的文件类型来指定 response.contenttype 。(参考oschina的这个网址可以找到大部分文件类型的对照表:)% A) |; U: E" t
3. 在每次写完response时记得调用 response.flush()1 T0 T: z) _+ e' E0 y; z) _% U
4. 在循环下载的过程中使用 response.isclientconnected 这个判断可以帮助程序尽早发现连接是否正常。若不正常,可以及早的放弃下载,以释放所占用的服务器资源。
6 t4 P2 a# \/ H7 p5. 在下载结束后,需要调用 response.end() 来保证当前线程可以在最后被终止掉。5 F3 X$ I" }0 }7 ^ |8 J( [ n
代码如下:+ n! {( Q1 {* X C' d2 |
using system;: S- p! _) z) m$ e/ N+ J
namespace webapplication1
# | ]/ d# K/ T, B+ x% u1 V: O{
% r4 f( N, t8 R' ^7 Bpublic partial class downloadfile : system.web.ui.page* c d$ Q% W4 b7 f* |0 `" A' R
{% s" h, p/ V$ E( z* ^' V, I) c
protected void page_load(object sender, eventargs e)
# Z' ~. S5 z$ t; Y{0 C! N6 r7 L6 L4 x! N
system.io.stream istream = null;' Y1 G- p9 Q4 b9 o/ E9 I: g/ _7 g3 |8 d
// buffer to read 10k bytes in chunk:
4 u1 T+ F( a3 E, z7 v- I* B5 Nbyte[] buffer = new byte[10000];5 m( a, A: n& i2 I% y! b. [
// length of the file:
4 z" ?: z( B9 `9 m, Z& v mint length;0 a7 a/ V1 I' E- h- n8 a
// total bytes to read.8 E5 n3 }- e+ [2 l: i& j. `7 o8 z9 u
long datatoread;
4 k# V; [- s% J1 T: z3 _: L// identify the file to download including its path.
# z2 \8 d) d( c( M$ M! I+ Rstring filepath = server.mappath(/) +./files/textfile1.txt;8 U( N- m0 _7 ^0 ?/ i& ~4 t
// identify the file name.. [5 M) n. w( I
string filename = system.io.path.getfilename(filepath);$ X: T! f1 w1 O- k3 M; K/ O
try
5 Q: d. @ D$ ^, d{$ C: G# P$ u0 Y% N$ y. a, P8 `
// open the file.# Z f' u I' Y9 c
istream = new system.io.filestream(filepath, system.io.filemode.open,
6 y8 T$ f; r5 F: qsystem.io.fileaccess.read, system.io.fileshare.read);
6 E4 ?) X/ q3 U8 \1 q// total bytes to read.
3 a1 N: U; g0 L( `: k7 cdatatoread = istream.length;
1 ]% A* R% J" _8 {9 ?8 Oresponse.clear();- I+ B0 }+ d& F! \7 [; r; b/ \
response.clearheaders();
% o& ?% ~- ~5 Wresponse.clearcontent();4 [2 e) a: X; W4 ?3 c6 q1 D) ]
response.contenttype = text/plain; // set the file type; n! Z3 U- t1 P- [& ]
response.addheader(content-length, datatoread.tostring());2 K7 ~& w8 D- @- X" o* u" ^
response.addheader(content-disposition, attachment; filename= + filename);* `" h8 N4 G- w6 s' Z2 q c8 \( ] m
// read the bytes.
3 t2 Y6 `4 q; z' x) p; Pwhile (datatoread > 0)
{0 U$ \ m, l; v& |9 T- k2 Q- t8 `{
; w! u2 Y# m9 l" j! h6 P, F// verify that the client is connected. E% k6 d" ~2 c3 e ^ |& o
if (response.isclientconnected)( E/ Y9 T5 n# y: Y' M
{
7 @9 L9 ?/ y8 s- I5 O2 q// read the data in buffer.
1 [& h0 i0 S/ R6 B6 Mlength = istream.read(buffer, 0, 10000);3 Z7 u) H* L" Z7 {
// write the data to the current output stream.
2 [- g" z5 H7 n7 O0 t I. ^response.outputstream.write(buffer, 0, length); L8 C! N( n8 C# D9 |& z3 D' n
// flush the data to the html output.
. F( L" y5 m( n6 Jresponse.flush();
. l1 ^2 u: d0 V, ]) Cbuffer = new byte[10000];
2 i# X8 a3 ?; l6 S- L% A! [datatoread = datatoread - length;
% A; w2 b9 i& F& i5 q}
: G$ I9 J( [0 J! V9 w' i1 o! Oelse' B- X; r; J% v4 x. V4 _
{
7 M8 b7 M' H7 _1 b, W8 \; z// prevent infinite loop if user disconnects7 ]1 R: P0 g6 O/ ?- G3 i) L
datatoread = -1;; Z0 N& R1 l- R; N
}- H, v, S8 F& M: p% Y3 E, q* D( T
}
5 n4 }/ X1 l F; W! }+ ~ j}( Z; Y/ G# t# V' h# W
catch (exception ex)& @3 r6 }9 O7 I& t
{0 A% N+ j! T3 X% c
// trap the error, if any.1 t) d7 e* _1 S; R
response.write(error : + ex.message);) S2 H7 J. l, @1 J
}8 w& |* ^& ^5 O+ f$ Y5 u9 e
finally7 W) M) ^- b# }4 W/ a4 |( I! o
{/ i' n/ @( F) I, @ L
if (istream != null)
# B8 V$ m8 K* y' n9 t- [' W* c{! S) f$ b8 _) m" D3 @ _
//close the file.
" c4 \( n l9 f7 H0 k* E Histream.close();
( o" _8 n' a$ G! y k; B3 n7 A}
2 v" J2 g$ z$ f6 D z( Fresponse.end();
8 k2 X( i) Z( Y+ ?9 \+ v}
# _1 ~2 k7 \' H}
2 W; O, M# `% Y$ J0 J; u/ _}/ Y3 }. E9 t3 F1 H* K+ E
}
$ [/ e: g- u1 ~) @/ a希望本文所述对大家的asp.net程序设计有所帮助。' v6 @3 x, D+ {. ~1 p: d; X
; f5 m. A& s* Z1 ]+ O W更多网页制作信息请查看: 网页制作 |
|