本文实例讲述了asp.net下载大文件的实现方法。分享给大家供大家参考。具体分析如下:
5 f k3 ]' R" {8 H; P当我们的网站需要支持下载大文件时,如果不做控制可能会导致用户在访问下载页面时发生无响应,使得浏览器崩溃。可以参考如下代码来避免这个问题。$ M: M' Q5 i! G. P, G4 ?; n
关于此代码的几点说明:2 o7 v7 m8 a4 ~) r
1. 将数据分成较小的部分,然后将其移动到输出流以供下载,从而获取这些数据。$ p4 c6 X" H% P" g2 [6 d/ ]8 j
2. 根据下载的文件类型来指定 response.contenttype 。(参考oschina的这个网址可以找到大部分文件类型的对照表:)! x+ V6 k3 [8 Q- I) l3 `6 H
3. 在每次写完response时记得调用 response.flush()
) o6 _# d$ B- ]& k( h# J& Q" s4. 在循环下载的过程中使用 response.isclientconnected 这个判断可以帮助程序尽早发现连接是否正常。若不正常,可以及早的放弃下载,以释放所占用的服务器资源。9 u# P$ L8 M2 D. ]; S
5. 在下载结束后,需要调用 response.end() 来保证当前线程可以在最后被终止掉。- G2 Z6 l( {* M1 @! y
代码如下:
, m$ }4 _4 v; Kusing system;$ O+ T. B+ [7 {4 i, h
namespace webapplication1
0 C) V l) h* T6 M) t7 ~) E6 |{) J/ |% m" o4 ^, R3 O$ W6 F
public partial class downloadfile : system.web.ui.page. L! q: C( M0 b0 P
{% [( H& y) }) d
protected void page_load(object sender, eventargs e)
- z4 p J9 ~# _{/ I4 Q6 ^0 A! a7 H* e. l
system.io.stream istream = null;
1 T Z* t k$ g0 I// buffer to read 10k bytes in chunk:
! v- _* D" ~9 O# @, Abyte[] buffer = new byte[10000];7 e2 l8 Q- R" y, ^3 h! y) K
// length of the file:% l1 X4 W4 A! G: O7 S3 h$ a$ x
int length;
% J7 Q3 Z, i3 @9 a* ~7 v// total bytes to read.; \$ v8 H2 {9 Z% ~
long datatoread;# O$ G/ w6 s/ D0 i! j
// identify the file to download including its path.# A8 X: i/ {) q# c2 H* F! i
string filepath = server.mappath(/) +./files/textfile1.txt;3 k* Q" b5 ]# t3 |/ {7 t% f6 s
// identify the file name.
5 A5 R; |' ?7 M5 [" wstring filename = system.io.path.getfilename(filepath);
4 g4 R* J5 W; h' b4 Z$ Atry/ m" K& Q# ^5 G/ m7 I
{
( Q+ r/ R" a7 N. L5 i// open the file.
" ~( p1 K" Q' h8 iistream = new system.io.filestream(filepath, system.io.filemode.open,; a) o- W% F) [4 Q1 i
system.io.fileaccess.read, system.io.fileshare.read);; |1 m' s# p/ U6 ]( C" _6 b: u# l% `
// total bytes to read.
! F2 r2 c* S1 R7 q/ u5 _1 |- ~datatoread = istream.length;
' `- ~( U7 e+ bresponse.clear();" S. v2 l; V) t" v0 H6 i7 ]6 x
response.clearheaders();
" Y1 x7 U& s1 {response.clearcontent();
, E* l( P) s, b+ P- H: H3 }* kresponse.contenttype = text/plain; // set the file type
( b4 {# M0 y$ v" ~' }2 r9 {response.addheader(content-length, datatoread.tostring());( W& f3 F- i$ D, ?( w! j8 V6 \
response.addheader(content-disposition, attachment; filename= + filename);
: q: \# A7 c, H& `( h U. v// read the bytes.) `; c* z3 ~' v. [/ [3 Y* c. _$ z) w
while (datatoread > 0)
1 F" l# i. T" H7 S1 r1 y{
# N6 v1 M6 w8 I5 o; C; k3 i// verify that the client is connected.& h$ c' p7 H Y; _+ c8 Q
if (response.isclientconnected)% _9 `9 N$ P5 \% h. C- Z
{
5 i& H/ l, T1 C5 [8 l// read the data in buffer.2 p& G& H' X& f `& b$ {
length = istream.read(buffer, 0, 10000);+ J& d7 v0 e% n5 R6 f7 I
// write the data to the current output stream.
) ~# e" }* {1 n& y8 y) sresponse.outputstream.write(buffer, 0, length);
. T; ~3 s' Q) Z# v. O// flush the data to the html output.
5 H. N; B6 R0 x O/ O# Uresponse.flush();
: R) f/ J" v. ^1 F+ y) ~ [: `: Pbuffer = new byte[10000];% {& N6 b+ n5 J R0 Z$ c
datatoread = datatoread - length;, Q2 u+ U0 O* N2 |
}
( d; O4 T* H4 h3 delse
; T$ S* }7 r5 r6 f4 q, `{9 K& |6 x5 d/ j
// prevent infinite loop if user disconnects! P+ ^/ ~4 w8 z! j) j
datatoread = -1;3 c! X% g: K% L' U7 E- f
}
( P9 i5 s. I0 Z}
: `; w5 e2 ]4 V}
1 f4 j6 e! l6 `6 e4 n& t% L* K8 q/ X/ D4 }catch (exception ex)8 F( n; Z0 l& ~. E0 K9 q0 q, i7 f
{
Z2 c( V/ x" N. c2 z) q0 g// trap the error, if any.- Y9 {* @% y2 p" B6 j; j9 A9 n
response.write(error : + ex.message);2 u4 S& b& j% M& a8 B
}' K3 @: q$ w6 R& a( u
finally! f4 Z6 k$ o l4 ]% n t
{) j& e* H/ |" [+ Z4 O
if (istream != null)2 [7 N, f7 |' r. z" {
{; ?' f" ^; h9 x! O0 T
//close the file.
6 h: j+ ?0 R$ zistream.close();
. p* M5 g& m% W$ l8 Y}
# _" \' w! v% w+ t& Uresponse.end();
( \! Y( Q$ T; s- k}
( t7 b# {9 u& d% ~7 z+ ~: ]; @}; C% ?$ |/ O! f9 y
}( \; {6 R6 W3 a3 H/ M7 |
}6 Y L# n8 T( {
希望本文所述对大家的asp.net程序设计有所帮助。
. w! q* V/ Z* e
* o& W; z8 @ `6 U0 i- L更多网页制作信息请查看: 网页制作 |
|