得知互动
标题:
asp.net下载大文件的实现方法
[打印本页]
作者:
swmozowtfl
时间:
2015-7-31 22:04
标题:
asp.net下载大文件的实现方法
本文实例讲述了asp.net下载大文件的实现方法。分享给大家供大家参考。具体分析如下:
" U" A- `2 Y( P! T- l
当我们的网站需要支持下载大文件时,如果不做控制可能会导致用户在访问下载页面时发生无响应,使得浏览器崩溃。可以参考如下代码来避免这个问题。
5 j9 T( m% F& l. v' T, N
关于此代码的几点说明:
( H+ a" X6 E, A( M, D' M
1. 将数据分成较小的部分,然后将其移动到输出流以供下载,从而获取这些数据。
6 I: g5 x/ i+ O) E2 j
2. 根据下载的文件类型来指定 response.contenttype 。(参考oschina的这个网址可以找到大部分文件类型的对照表:)
. ~7 t( n+ u( d+ _6 J; z W& b
3. 在每次写完response时记得调用 response.flush()
7 s5 V1 C; d9 k5 D, ]: q6 k
4. 在循环下载的过程中使用 response.isclientconnected 这个判断可以帮助程序尽早发现连接是否正常。若不正常,可以及早的放弃下载,以释放所占用的服务器资源。
: ]- L6 m( w; \$ M$ S
5. 在下载结束后,需要调用 response.end() 来保证当前线程可以在最后被终止掉。
" S( C8 k: q& E$ o$ c6 S, a9 @# v: h
代码如下:
( O' w) V3 G$ r8 x2 \
using system;
) f# v1 m. p; J- M# n' r. s
namespace webapplication1
5 H$ i. A+ q9 l8 n
{
( K R: q" D8 \
public partial class downloadfile : system.web.ui.page
" T$ U/ z+ d4 c/ T
{
4 `+ Y0 k' O- W: j% O
protected void page_load(object sender, eventargs e)
1 [6 h% K$ I% A5 T" h6 `" i, |
{
( W: k* J% F/ F! r6 Q- a
system.io.stream istream = null;
6 a7 W) s( K7 V- ]7 U1 ^
// buffer to read 10k bytes in chunk:
; }* r1 e) ?! ]$ j7 f. V
byte[] buffer = new byte[10000];
" \" F% U2 }2 M( V' O1 ?* ?; ]
// length of the file:
3 [. V e( J. q4 P4 h7 G3 y) D' C
int length;
* m# c& d+ d* R6 B* U
// total bytes to read.
$ ?2 b3 B& w, r0 \( U0 E
long datatoread;
" ~8 s: L. S5 |2 {# I
// identify the file to download including its path.
1 q* _* S0 ]5 _, i: H1 d
string filepath = server.mappath(/) +./files/textfile1.txt;
1 ^: f7 [% @( q. h
// identify the file name.
. B% k! D& |: M4 C8 W) C
string filename = system.io.path.getfilename(filepath);
; J# H4 y1 r, G' X6 m
try
9 K+ f5 c0 W, W5 \2 v
{
" U1 A. \- h- I: I
// open the file.
, s7 o# m) e: V8 d6 B
istream = new system.io.filestream(filepath, system.io.filemode.open,
# v! k* h9 m! w) k4 N2 k+ D
system.io.fileaccess.read, system.io.fileshare.read);
6 K6 J* O. `! ~. `
// total bytes to read.
3 q! A4 \& R3 w. @5 D
datatoread = istream.length;
! [# C. f6 F) j
response.clear();
+ t% a& y' T* r1 |+ ^; z5 q4 o
response.clearheaders();
) H0 c: D& x9 {! q% B$ Q
response.clearcontent();
- ^6 k, n: i1 \) j0 Z% [5 `
response.contenttype = text/plain; // set the file type
8 t% j1 P" P6 y
response.addheader(content-length, datatoread.tostring());
1 m5 k, l' \: y, i* g6 S7 z7 H
response.addheader(content-disposition, attachment; filename= + filename);
* E+ f: `: ^. P% y
// read the bytes.
) e8 P( m7 N3 h! Z# j
while (datatoread > 0)
/ f g5 j" K4 a+ @
{
2 i: d7 d6 |! ~ n# w6 K, f
// verify that the client is connected.
/ T. S+ z& M4 o4 C3 R0 f
if (response.isclientconnected)
+ X: j$ I' Y9 R& q5 M1 d
{
! W& ?4 Y1 K* ^) Z# R
// read the data in buffer.
5 W d0 X; W S/ Z
length = istream.read(buffer, 0, 10000);
1 `( C. D e' R/ ~+ A6 [
// write the data to the current output stream.
$ t5 l2 {$ k4 O6 O! U4 E9 ]
response.outputstream.write(buffer, 0, length);
+ Q& w3 H# s/ p! x+ e- B/ G- |1 `, `
// flush the data to the html output.
/ S7 K Z4 o: y( x' W- G: r
response.flush();
, p: _) u& V( B9 }2 _" l
buffer = new byte[10000];
) U. S' T$ Z- u
datatoread = datatoread - length;
! o; i$ m% J7 Q
}
5 b% H) ^, l( j
else
: R8 M/ |. l- ^( G1 O8 n# Z
{
5 G+ y4 e: X! k1 A" p
// prevent infinite loop if user disconnects
3 n7 _! n }$ {+ o3 U8 i% F
datatoread = -1;
/ h6 g" ~& U: M+ X' R* m
}
6 P" y, f$ t3 w3 I# `0 @6 o& e: }
}
- N. U, A& O4 g* D5 g' b! j
}
w6 _4 }9 I4 B: F
catch (exception ex)
1 n8 y! u8 e" D, R0 V4 x
{
( o0 @, \1 n2 b2 y0 u( j+ V& W
// trap the error, if any.
# C3 \5 }, O: W
response.write(error : + ex.message);
/ u& o. m- G: K1 J E& k4 R
}
! N. N2 I' d7 o* w
finally
& c! Z8 o5 b: T
{
$ ^- T5 b9 t( Y* d
if (istream != null)
* O6 N) Z6 v3 p8 }( T
{
5 ?8 ]6 V2 _7 [- J
//close the file.
* q4 T0 x9 y0 l) A+ V
istream.close();
2 Y, e, L3 E1 I, }
}
+ d- q" ?! f, J6 Q, }' q+ h" k
response.end();
- [) x! W1 K* Z, v# ]: w: v2 x
}
9 z" X2 y: X( @$ S6 O
}
/ t% l h5 T+ ?) k
}
1 A; b5 a0 y [; |! ~$ g6 `
}
3 x- ^8 i2 C& @% @3 R) s/ {4 C. `
希望本文所述对大家的asp.net程序设计有所帮助。
6 Z/ o6 O8 C* s% e6 w" X
2 o! {4 j3 i9 s8 k$ E& E l
更多网页制作信息请查看: 网页制作
作者:
seazvyt
时间:
2015-9-13 18:22
对于这种刚发的帖子,我总是毫不犹豫的顶了。如果火了就是个前排,可以混个脸熟,说不定谁好心就给粉了…稳赚不赔;如果沉了就感觉是我弄沉的,很有成就感,还能捞经验。
作者:
mwxny
时间:
2015-9-13 18:22
这篇帖子构思新颖,题材独具匠心,段落清晰,情节诡异,跌宕起伏,主线分明,引人入胜,平淡中显示出不凡的文学功底,可谓是字字珠玑,句句经典,是我辈应当学习之典范(不好意思回错帖了
作者:
Mqokjdvq
时间:
2015-9-13 18:23
感觉这个论坛的站长太牛B了,好强大啊
作者:
buingeEvineus
时间:
2015-9-13 18:23
这个论坛值得推荐,给了我们这么好的一个平台
作者:
wwzcdenleclv
时间:
2015-9-13 18:23
这就是我斗胆的一点粗略分析,每天睡觉以前,我都会把您的帖子再三拜读,拜读。
作者:
seazvyt
时间:
2015-12-25 20:48
老大,我好崇拜你哟
作者:
mwxny
时间:
2015-12-25 20:48
这个论坛值得推荐,给了我们这么好的一个平台
作者:
effoggikeftor
时间:
2015-12-25 20:48
只是本着“看贴(虽然看不懂)回贴,利人利己的中华民族优秀传统美德”,顺便赚点积分。
作者:
effoggikeftor
时间:
2015-12-25 20:49
cd:遮~~~~~~
欢迎光临 得知互动 (https://bbs.dezhifl.com/)
Powered by Discuz! X5.0