本文实例讲述了asp.net下载大文件的实现方法。分享给大家供大家参考。具体分析如下:
* R3 \! E5 `4 L, E* C当我们的网站需要支持下载大文件时,如果不做控制可能会导致用户在访问下载页面时发生无响应,使得浏览器崩溃。可以参考如下代码来避免这个问题。
% e0 s9 I# X# b: E e" o; C关于此代码的几点说明:
4 s! t# G, C& U/ _' Q+ J+ m! j1. 将数据分成较小的部分,然后将其移动到输出流以供下载,从而获取这些数据。
* f) p3 f8 J, k) ]2. 根据下载的文件类型来指定 response.contenttype 。(参考oschina的这个网址可以找到大部分文件类型的对照表:)
& M- v$ m2 Y0 m- D9 M3. 在每次写完response时记得调用 response.flush()# S% p6 c- b" ~
4. 在循环下载的过程中使用 response.isclientconnected 这个判断可以帮助程序尽早发现连接是否正常。若不正常,可以及早的放弃下载,以释放所占用的服务器资源。
$ I; ^5 y. I6 n6 }. D! Q6 e4 j5. 在下载结束后,需要调用 response.end() 来保证当前线程可以在最后被终止掉。& p. _: ?: \* h# Y1 b/ V- Z
代码如下:6 O/ k- N. ]: U2 X
using system;) [1 p4 C% {4 X8 m' x7 z- U- T# t
namespace webapplication1
) [! H& W0 w8 F{3 `7 |" y: I1 ~, }
public partial class downloadfile : system.web.ui.page
/ L5 `6 X( V! L% o! v4 @9 T* |{
( _+ h& h2 M9 n5 A; pprotected void page_load(object sender, eventargs e)* N p, s- Y5 u+ g1 }& ?3 e
{
& V, f" h8 f) f' R3 p0 _system.io.stream istream = null;
/ w% m. r' m; a: q6 F- Z: w8 v// buffer to read 10k bytes in chunk:0 B% ]3 s- I; q) o6 o3 f8 t
byte[] buffer = new byte[10000];: |/ }1 c y2 r3 h; j
// length of the file:: M$ X3 r- F0 R3 h1 x
int length;
7 J* f& c$ J2 Q# M// total bytes to read.
2 Z) w9 T$ Y1 o0 ^% v- ulong datatoread;
) `8 h/ F6 ]. j( f' G* N// identify the file to download including its path./ l" }, }9 X9 f4 d; r( a
string filepath = server.mappath(/) +./files/textfile1.txt;
3 M& J- X! H! z' W0 e2 k; d3 W2 ]8 J// identify the file name.) Z, k4 l4 h2 H8 f# g& R
string filename = system.io.path.getfilename(filepath);
2 x5 L6 q4 k8 d+ j! |3 \try9 i) X5 p% j* j" q- N C, u. {
{+ i+ ~/ T2 Z% o3 J
// open the file. ~4 b n$ }5 n4 Q0 i$ J
istream = new system.io.filestream(filepath, system.io.filemode.open,
8 h! x5 t! z9 bsystem.io.fileaccess.read, system.io.fileshare.read);
! p( M/ a' G+ P2 I// total bytes to read.
1 z" m& W' m \& \4 A; Tdatatoread = istream.length;
! n( p4 A$ X) s' h" {+ S3 I' [response.clear();0 B' B: \: G& f# Y# o0 I4 l l, q
response.clearheaders();
6 X t; }1 y! ~$ q$ O) e. S1 Kresponse.clearcontent();
: e, j4 l0 B6 A1 @8 d [' P8 Bresponse.contenttype = text/plain; // set the file type% U% N( ]7 \" b" q, J- n: H
response.addheader(content-length, datatoread.tostring());
# c1 ], t7 @1 e* S6 z$ m: Y- _2 cresponse.addheader(content-disposition, attachment; filename= + filename);
) [% [4 \1 g8 a# ^& }6 M4 \# F8 Q// read the bytes.
# i* Q% R: Y. {- v7 Fwhile (datatoread > 0)
' ?, F; q0 s* V5 Y% `1 c- Q q/ ]{
; E5 s0 T/ ?: k" ~6 h' F// verify that the client is connected.
8 O* @: b6 N. i8 Y6 o/ ^4 Bif (response.isclientconnected)
U; w4 k4 p. U' L# f{2 S) I/ |+ [+ o, H3 W: y2 M
// read the data in buffer.
* O* |) |7 L3 d8 b6 `% g5 ylength = istream.read(buffer, 0, 10000);; z0 r# G; a) i: Y$ b" p
// write the data to the current output stream.
- c" ]5 V. c0 ]4 q( z9 h# qresponse.outputstream.write(buffer, 0, length);" U% l% T. P$ o* J- V& R2 u
// flush the data to the html output., ]# [" h4 t% K( U0 x3 k, E
response.flush();' [5 W& ~0 K h4 J- }
buffer = new byte[10000];
* K F9 b$ P2 `6 p5 ^. @7 P" [$ Jdatatoread = datatoread - length;
8 _ j$ a/ _5 C}# N. d3 N( s; u _: u- T
else
5 j4 ?8 U& i# J, ?{% v$ n! B2 N9 z g2 P* Z& i
// prevent infinite loop if user disconnects5 W% R$ r" D8 m9 N8 v2 B( b# E/ u
datatoread = -1;
* y! y% X/ P* y& I4 O: p l! F, G}. t9 k' N3 { {8 I+ S5 S( O( C
}- m% o- [1 v# o l6 \
}2 \1 {* M. E1 f5 O5 U: a! O
catch (exception ex)
3 A+ Q q; S8 ?{
7 M* K# E/ P8 m1 i! `- F// trap the error, if any.4 i, C& s0 | t# L! n; _
response.write(error : + ex.message);' ?' C/ M( H# [' _, P
}, H3 c. u+ C1 X6 V/ l5 J
finally
$ \0 W6 S: z$ H" l. i/ _, ?) W{# Q U. E# j" v6 p: m
if (istream != null)
: p( \1 l+ l* Y, l- O0 y2 P0 p{
7 J/ A0 |8 L- J$ y: a$ D: L//close the file.
% s. G w# V+ y8 }' fistream.close();
+ g! |( ?3 C; s, P5 F; ^}
& o& z3 l: N' o5 c- Z. Uresponse.end();
0 R9 T7 J2 M" x4 O}
3 Q% D4 g+ x0 K( w( S}9 ]9 N! h z1 W6 `
}
! t8 `, S. m' i}
" C( d7 q' D& l [8 H1 [. Q) d& w希望本文所述对大家的asp.net程序设计有所帮助。
; z! t3 m# J8 D- P/ w" Z( ~
) U7 u, Q& S& K/ W! H) S9 |更多网页制作信息请查看: 网页制作 |
|