本文实例讲述了asp.net下载大文件的实现方法。分享给大家供大家参考。具体分析如下:
$ j- J) }3 l$ _. u9 L当我们的网站需要支持下载大文件时,如果不做控制可能会导致用户在访问下载页面时发生无响应,使得浏览器崩溃。可以参考如下代码来避免这个问题。$ ?$ X8 r I( f% k( a7 R
关于此代码的几点说明:
/ r" q* H/ ^' r) Y' m5 J6 ^4 Z+ z1. 将数据分成较小的部分,然后将其移动到输出流以供下载,从而获取这些数据。
6 N% D/ S% m8 n8 U- b2. 根据下载的文件类型来指定 response.contenttype 。(参考oschina的这个网址可以找到大部分文件类型的对照表:)
$ u5 K7 p+ y1 z' [3. 在每次写完response时记得调用 response.flush()
+ P. q) `) [1 E- g# a$ t9 m4. 在循环下载的过程中使用 response.isclientconnected 这个判断可以帮助程序尽早发现连接是否正常。若不正常,可以及早的放弃下载,以释放所占用的服务器资源。
4 Z3 z5 x+ o3 I1 a5 G; p5. 在下载结束后,需要调用 response.end() 来保证当前线程可以在最后被终止掉。
0 j/ o& a2 J8 C G2 D" a. o代码如下:
8 ]0 T7 x5 f+ }& vusing system;
$ L+ z3 X3 H4 J& Anamespace webapplication1
$ {6 g" X0 d" N{
5 u* g: D5 P* b7 i0 vpublic partial class downloadfile : system.web.ui.page
/ {# v! ^0 R( b. X{
# P6 C* K) e2 E: eprotected void page_load(object sender, eventargs e); M2 p6 ]2 w* |% r* K- [4 c
{) Q( d" J4 |, x( k- @$ [
system.io.stream istream = null;
$ v5 `. _ h' X0 V- ~// buffer to read 10k bytes in chunk:
2 ^4 R7 r3 X2 y/ o9 ^. Rbyte[] buffer = new byte[10000];
' y0 d. V# }5 ^* H// length of the file:
* k: O) d( ^! _7 y2 V5 a8 {6 D( dint length;
8 h+ U/ s* _/ A" o: q// total bytes to read.0 l# v! |1 R8 m
long datatoread;
) g6 `1 i: y5 T// identify the file to download including its path., l5 Y, s6 N% k4 g" _% a9 K, @
string filepath = server.mappath(/) +./files/textfile1.txt; r" |, F( B, |5 f7 r Z( A8 e
// identify the file name." f$ X k1 W! v0 ~6 ?8 ^
string filename = system.io.path.getfilename(filepath);1 {! }8 V$ {* G+ z( e
try
1 a+ _5 z! U6 H# U# [{
0 {* h$ H5 j4 O// open the file.
5 t4 ~8 ]! {! T. g4 a5 _% I1 {, vistream = new system.io.filestream(filepath, system.io.filemode.open,% j$ |! g$ {- l, C: F! a2 b
system.io.fileaccess.read, system.io.fileshare.read);6 O: Q8 j' q+ d% j9 ~
// total bytes to read.: _- j* v5 F; f2 }6 r7 O1 p3 d2 P
datatoread = istream.length;; d$ d$ B# V! q' @5 ~
response.clear();
1 O; r2 A) w5 X* F' b/ r6 f: ~" N& oresponse.clearheaders();
8 j( w0 w, ` M# V3 ^response.clearcontent();
9 Z# J& G+ i# A% u- Q; {' H; rresponse.contenttype = text/plain; // set the file type
* {8 Z, Q* E+ O6 Tresponse.addheader(content-length, datatoread.tostring());) ]' z2 m: w4 Z
response.addheader(content-disposition, attachment; filename= + filename);% O- X8 a4 r3 }
// read the bytes.* X- u4 } ]! `3 U" }) g T/ P) P
while (datatoread > 0)
% M8 G! b, w+ B* K2 E' \7 H{
" r5 n: {$ e* J// verify that the client is connected. `4 N! }1 b* H% i+ ^
if (response.isclientconnected)
, F$ R0 |$ z3 C; ]5 `* ?8 ^3 A{2 f! N0 X: C: i& [7 O
// read the data in buffer.
- ]" U8 ^: h/ ?* Olength = istream.read(buffer, 0, 10000);) Y9 r8 q D j, R6 i- c
// write the data to the current output stream.+ ^/ U$ m/ R% Q e$ J9 _
response.outputstream.write(buffer, 0, length);
. b8 ?- i/ M* v! W8 G// flush the data to the html output./ [$ ]$ M' P* s; H' U% b( D
response.flush();. [5 |& b# } g, d4 o
buffer = new byte[10000];2 ]) N; c+ V" B- Z- X c
datatoread = datatoread - length;8 U* V% Y1 B6 D$ }- C6 B9 e! a( \
}
# H' i6 z. a) i/ Melse! O3 r& s8 O' e! q' X3 A" \
{& ^# Y5 ]' G: |6 X
// prevent infinite loop if user disconnects: c3 U+ n Z: g" V
datatoread = -1;# G2 [. V5 l/ [
}
8 i* n {$ c' a" W+ n6 O}) y3 Q: H1 s" J1 v7 |8 F
}
0 ^, i7 V4 ^. K6 K5 Z6 h& R3 pcatch (exception ex)' f' q: F( I& P! m) K8 R" L
{' v$ R3 C3 F" D* Z
// trap the error, if any.9 P, y: D& D. `- ?9 J- |) R
response.write(error : + ex.message);
' B: O' v% ]4 J1 e% V' O6 F% T}4 G* p$ ]9 g+ a
finally* m: l7 q* b& r; d; z* T$ M
{
7 ]9 O- ` R( v& E3 u$ S' eif (istream != null)
( A. u3 {' A+ Z3 k9 G{
4 q: U0 S6 `+ v0 a: L/ a: V//close the file.2 y8 N, X M( F
istream.close();7 K: M2 d6 x2 R# D
}6 h8 Z+ W* Y1 T; p+ r5 C/ N7 I
response.end();- x9 ~* W! C& m* _) R6 l2 w. C
}
5 j0 u4 d0 u# S2 s}
3 [4 }0 r5 G) w1 l; K- U}0 | U2 q# W y( a
}( S9 C4 E, T7 k3 z X; J' `
希望本文所述对大家的asp.net程序设计有所帮助。
) R( R- W) t5 ]) d$ b
% O: _/ K1 W9 L+ n' ]1 r% ^( q% W更多网页制作信息请查看: 网页制作 |
|