本文实例讲述了asp.net下载大文件的实现方法。分享给大家供大家参考。具体分析如下:* _2 L$ `$ G! _: z+ n3 Z( Q
当我们的网站需要支持下载大文件时,如果不做控制可能会导致用户在访问下载页面时发生无响应,使得浏览器崩溃。可以参考如下代码来避免这个问题。
; T K3 [2 W2 A, X3 e关于此代码的几点说明:
% w$ b0 N1 B, _/ Y' K ?& Z1. 将数据分成较小的部分,然后将其移动到输出流以供下载,从而获取这些数据。
; Z8 Z% D% k; G0 w f0 A2. 根据下载的文件类型来指定 response.contenttype 。(参考oschina的这个网址可以找到大部分文件类型的对照表:)
8 P! y6 q, R: }7 F3. 在每次写完response时记得调用 response.flush()
2 N e& k# R$ ~4. 在循环下载的过程中使用 response.isclientconnected 这个判断可以帮助程序尽早发现连接是否正常。若不正常,可以及早的放弃下载,以释放所占用的服务器资源。
# H5 N. u& L# d! i c g: ?& I5. 在下载结束后,需要调用 response.end() 来保证当前线程可以在最后被终止掉。
& _" @5 F \; |3 A代码如下:
) r$ z! q% W- Z& c7 ]3 \0 L8 Susing system;5 w) ?* g6 k; o; _' D9 F; i1 y2 d/ W- `
namespace webapplication1) S2 k0 Y1 T7 x$ Z7 t
{, L/ V* e: W" @% _/ ?
public partial class downloadfile : system.web.ui.page# x! p3 ^" V- M
{# c c: }7 W3 H+ e! y
protected void page_load(object sender, eventargs e)+ j' ^" O# R0 H/ h" S& e" U+ c
{" Z& F; a: H" f1 d9 Q
system.io.stream istream = null;7 W' r( q$ o7 }" S. C X
// buffer to read 10k bytes in chunk:
. D6 V( ^& C8 s% p" ibyte[] buffer = new byte[10000];
8 k$ G- V( A/ Y/ t: k2 N% ~// length of the file:5 \$ H8 h2 ]% d
int length;9 t/ j3 p, A" ?$ c* c3 S7 b9 j W
// total bytes to read. s* }3 e! r4 Y; t) w6 n8 {8 r5 I
long datatoread;+ w# a& F9 ?& ]' t
// identify the file to download including its path.
; B/ \. t1 f* H! D3 I+ @0 Z4 s! Cstring filepath = server.mappath(/) +./files/textfile1.txt;
; u, U/ I. I* t. `// identify the file name.) H1 }& S6 P" a- o4 D
string filename = system.io.path.getfilename(filepath);4 l3 a- s1 x. |
try. }2 ^. b- [4 X- E
{$ k+ H& i+ P/ U7 ]& Q- D
// open the file.
) F+ j" E* z7 m# ^: X, h& Bistream = new system.io.filestream(filepath, system.io.filemode.open,9 }3 ]3 Z7 U# Y4 D; W/ [ s+ y
system.io.fileaccess.read, system.io.fileshare.read);
: @5 Q. c% D' k2 t" T// total bytes to read.
$ J! O9 {* R& G# T- c$ ddatatoread = istream.length;, ]: Y9 e% }; ]; b) x4 I% p8 r
response.clear();
* D0 K1 F; m7 K$ r. t( Wresponse.clearheaders();* J4 H4 ^# J8 M G% a2 k7 ]
response.clearcontent();6 k8 m c- K6 q
response.contenttype = text/plain; // set the file type
7 d4 j' V3 ^* I4 t4 U, O, F2 k4 K9 qresponse.addheader(content-length, datatoread.tostring());8 I5 W2 D3 T1 g, I
response.addheader(content-disposition, attachment; filename= + filename);) ?' c) q+ A* M
// read the bytes.
8 W7 w/ U6 l0 ]' }$ kwhile (datatoread > 0)
& P9 c4 X9 K! C- j( L ~/ f{8 O5 I2 [, `- u, n9 ]
// verify that the client is connected.1 S: G6 N3 q& Y0 ]
if (response.isclientconnected)
' K) O) }' R& i T/ z& P{
6 D3 F; H! `( O+ |. S. n// read the data in buffer.# W" P9 Z8 M/ d$ R( O# k- W
length = istream.read(buffer, 0, 10000);
. \+ Z# x' x) u. l/ g M S W// write the data to the current output stream.9 d2 ~6 D7 l0 c7 Q z/ v
response.outputstream.write(buffer, 0, length);. P" x1 e z8 A5 H" i
// flush the data to the html output.
4 `/ `$ N/ e) G- i0 G; D3 G& Cresponse.flush();! D) {) J- h1 W2 F+ }) c, p: G3 K; }
buffer = new byte[10000];
) Z" D7 E: U4 Ddatatoread = datatoread - length;+ W _( B) N7 G# Y: H
}
* a* G9 x* O7 W3 A1 D f1 Jelse
* ?6 U9 T% K# `{
! }1 c: `8 s* K// prevent infinite loop if user disconnects
) {6 f, ^; l( j5 odatatoread = -1;
* C) l* a3 p, p}
8 e2 Z6 H+ t* E! Z! v; {- |: H* X/ n}
2 w- j& Q1 F+ M; z$ Q+ {}5 {# T0 Q; g! |
catch (exception ex)
( }, T7 J6 N- Q# e{
' V3 L- H# Y& I+ T6 x" n$ f6 G// trap the error, if any.
9 z- u* W' e+ P8 j" p& a, Y8 K# Aresponse.write(error : + ex.message);
4 B# O8 R( R e0 h% @! p; A) a I$ V7 }9 e}
# q3 Q" [: B1 R# O( @) z% rfinally m8 \5 }; O/ e) U* D# q
{
% f+ m! R8 @1 v7 \' ]if (istream != null)
' _; N; F: z+ @, G# u{
b) p) V! Y7 P9 L, x' A0 |* k//close the file.
& C) E/ Z! i: N- g2 v% Listream.close();
; l7 Q" F1 x( D! _}
9 H# x* G' }; c' }6 U8 g% t& hresponse.end();
* R1 w8 \1 [# C7 J}
1 Y& O. B2 w7 b& N! I, C}
0 `2 `3 ^3 ~' K! H}) e9 _ B8 [" ~$ q% U) y/ X* b
}. |2 m5 j7 j' X+ K3 K% U z9 W
希望本文所述对大家的asp.net程序设计有所帮助。5 `( W( a: F: s% ^7 e! g! V
* ~' n* |& U- s: z k更多网页制作信息请查看: 网页制作 |
|