本文实例讲述了asp.net下载大文件的实现方法。分享给大家供大家参考。具体分析如下:4 L& L9 @, I M# m, p |
当我们的网站需要支持下载大文件时,如果不做控制可能会导致用户在访问下载页面时发生无响应,使得浏览器崩溃。可以参考如下代码来避免这个问题。
5 E/ l. B# @) P! P, q关于此代码的几点说明:
* K, |( M9 r$ {0 r2 u; k1. 将数据分成较小的部分,然后将其移动到输出流以供下载,从而获取这些数据。5 K& [4 m* M( N; i3 r5 g
2. 根据下载的文件类型来指定 response.contenttype 。(参考oschina的这个网址可以找到大部分文件类型的对照表:)
7 M/ h! q1 n8 ?3 t3 M9 n3. 在每次写完response时记得调用 response.flush()
- d, a) Y1 e3 s6 g$ F. y( ~4. 在循环下载的过程中使用 response.isclientconnected 这个判断可以帮助程序尽早发现连接是否正常。若不正常,可以及早的放弃下载,以释放所占用的服务器资源。
9 c& J3 q3 T0 h/ O& F5. 在下载结束后,需要调用 response.end() 来保证当前线程可以在最后被终止掉。
) N; g l* M; @0 D F代码如下:) ^/ f9 D) s# u7 R( }
using system;% f4 {1 C* u7 T; b
namespace webapplication1
* Z$ C! f4 S+ @, {4 o{1 J. e: h+ \8 n s
public partial class downloadfile : system.web.ui.page
' |, r: Z9 s$ d6 u [{9 U9 D1 ^2 Z j/ u. k' t, }! R$ x
protected void page_load(object sender, eventargs e)4 \- Y, D* o2 V O
{) } O; n6 ~4 x! v8 e& Z' v& ?
system.io.stream istream = null;2 ~- C+ y5 w' D% W0 f# o
// buffer to read 10k bytes in chunk:; t) ~. ]. t: k0 C; K7 c1 B
byte[] buffer = new byte[10000];
7 Y& S2 w, H6 P3 B0 }* Q// length of the file:
& h, V9 W4 B N0 A) [int length;
0 z6 U! y# N4 h// total bytes to read.
: V% A& u4 q; v" X& P+ O) Plong datatoread;. U% c, v& c e
// identify the file to download including its path." d, D8 k! p4 F: ]. _7 \! d+ B
string filepath = server.mappath(/) +./files/textfile1.txt;
* M9 ?4 X# p+ _8 u. ?3 ?( D8 F// identify the file name.
E( ^8 J$ Q5 ?2 D' d: h9 p3 Wstring filename = system.io.path.getfilename(filepath);
& ^: q8 W( B m) x/ F. ?2 Stry
. [& u' ~$ e. m{+ G, ~: \6 s' R
// open the file., H! X& x6 ?% t2 a& q. C
istream = new system.io.filestream(filepath, system.io.filemode.open,
( _$ l% i6 y! @; e1 x8 _% L0 @system.io.fileaccess.read, system.io.fileshare.read);
; u' x1 c2 B7 ^1 e$ Y// total bytes to read.1 ], D8 x9 Y3 ^" b8 I
datatoread = istream.length;
8 `# K4 l# A1 Rresponse.clear();1 Z. ~* \! q% f4 W
response.clearheaders();5 p+ ~+ R7 g& Z, l
response.clearcontent();+ E% D/ p8 I4 U1 u0 c4 U, Q
response.contenttype = text/plain; // set the file type6 U; E, h x4 m8 }( z, A$ D
response.addheader(content-length, datatoread.tostring());
& E9 i4 [0 e! z- B& lresponse.addheader(content-disposition, attachment; filename= + filename);* o$ ~- }% a3 Y! m, X7 a; t
// read the bytes." j# r4 l' ]4 k, W2 V# f8 d8 w$ {
while (datatoread > 0)
[( Q5 c& W* l! k7 [2 G) |2 I{
! E9 n0 r( f: Z, z. D7 b// verify that the client is connected.2 v, g% k1 m* e% {" \
if (response.isclientconnected)
% i4 F/ v* F* D' q{
, t/ o: {3 j. s' k// read the data in buffer.( e5 s w4 G( V
length = istream.read(buffer, 0, 10000);* n! D1 G$ I# z1 }- p
// write the data to the current output stream.
( z/ Q9 U7 s2 B) Kresponse.outputstream.write(buffer, 0, length);
1 J5 J/ q J$ H% c6 [4 c// flush the data to the html output.% j/ E0 O, c/ n
response.flush();
7 Z# V: U2 |2 l0 P5 `3 {% Wbuffer = new byte[10000];; D) W7 {0 ]6 y ?; N4 T. t
datatoread = datatoread - length;
4 G C# }0 i- x/ |( w) q9 T}+ q# ^% C1 M& B: N
else
9 N. M2 |+ T* z, \. I7 F }9 t{
( {) e+ d7 b$ Y/ J0 l& z// prevent infinite loop if user disconnects! @" r$ {3 v" G m9 v! [2 r1 b4 i
datatoread = -1;0 f/ i6 e8 x! L# G6 C! y5 q- P
}9 G, V) @0 m: s3 r- U0 p
}+ g: n1 ?1 E* R0 z( U9 f
}( V' Y7 r# X( j) Q& U
catch (exception ex)
) v; U; k- l7 Y9 O2 V; O1 y. e: x# A: U{9 J1 G5 d! k4 \/ G; w
// trap the error, if any.) l. z# S' G3 m9 V: ]. q, w1 _
response.write(error : + ex.message);
5 B- G" e3 F& @% o* N) ?, d% U3 f}
/ k! l, d# n% b! _, O8 [finally6 ~3 L7 W6 T' M& r+ {
{( o* ~: ]6 i2 ^6 L7 \. B5 `$ \
if (istream != null)
1 ~9 \+ ?. i1 L" F* Z{
6 R9 e' T2 A- u8 A) Z//close the file.0 |% M3 c& L# s% E
istream.close();0 C9 }6 G7 c6 t; u
}/ R8 g* z0 c8 e4 m t4 q. ~) Y
response.end();
- p, |5 ]# Q8 g+ Y( N}
" Q, P& t0 l4 q$ n# d/ ^! a}( [& j3 l* k Z; P5 o F
}
8 b" a* V( L1 Z' @+ {' b% |}3 c6 ~* Q6 ?5 J3 I0 _# H
希望本文所述对大家的asp.net程序设计有所帮助。
( ?# k! Z) U- [/ Z% L6 y( ]; s( a+ M- F( d
更多网页制作信息请查看: 网页制作 |
|