得知互动
标题:
asp.net下载大文件的实现方法
[打印本页]
作者:
swmozowtfl
时间:
2015-7-31 22:04
标题:
asp.net下载大文件的实现方法
本文实例讲述了asp.net下载大文件的实现方法。分享给大家供大家参考。具体分析如下:
, V" [' C5 S0 p1 x; w
当我们的网站需要支持下载大文件时,如果不做控制可能会导致用户在访问下载页面时发生无响应,使得浏览器崩溃。可以参考如下代码来避免这个问题。
2 ?6 k) R4 a; P) W. d$ ^
关于此代码的几点说明:
, g9 o# [* c n$ s0 G9 T: \8 T4 S a
1. 将数据分成较小的部分,然后将其移动到输出流以供下载,从而获取这些数据。
! A6 j" A9 A( e! Z9 n Q# w6 `0 i
2. 根据下载的文件类型来指定 response.contenttype 。(参考oschina的这个网址可以找到大部分文件类型的对照表:)
) H2 o7 B1 u( {4 {" a6 e
3. 在每次写完response时记得调用 response.flush()
* _- Z- B6 b2 T/ i! H# n6 h
4. 在循环下载的过程中使用 response.isclientconnected 这个判断可以帮助程序尽早发现连接是否正常。若不正常,可以及早的放弃下载,以释放所占用的服务器资源。
@/ l. u6 _/ Y7 z* b7 N
5. 在下载结束后,需要调用 response.end() 来保证当前线程可以在最后被终止掉。
0 `. v& ?" f1 _, C7 V, H8 q
代码如下:
+ U; b4 k. d7 U; T
using system;
* f7 Y& ?& f" {5 m
namespace webapplication1
8 _/ |) f( C; u$ y
{
2 v6 ^+ R. B9 p8 q% R
public partial class downloadfile : system.web.ui.page
+ z N$ f- M3 z5 @2 u" _" b
{
5 V5 L# H1 }8 d6 J6 W1 @
protected void page_load(object sender, eventargs e)
2 `& [9 o! G) `5 h( B
{
A7 z. D4 D9 h0 a* f; I2 ^; k8 S
system.io.stream istream = null;
# M3 O3 i% V8 I; N0 I' e/ J
// buffer to read 10k bytes in chunk:
# N! W4 g! Q3 N+ {1 D2 F
byte[] buffer = new byte[10000];
9 D x/ a0 U+ @ w/ y
// length of the file:
* S! I! L& I; \: u
int length;
& A" F0 O2 ~8 {4 q* J
// total bytes to read.
9 n3 i3 b+ S8 n4 N; W6 v! a- ^& ?
long datatoread;
8 @) o @) u) h3 e" `, G
// identify the file to download including its path.
" \0 `3 c8 C5 @6 N: j5 ~0 U
string filepath = server.mappath(/) +./files/textfile1.txt;
# `1 [8 C' f+ Q# D
// identify the file name.
8 B1 j7 m3 l0 P6 v: Y( u. n
string filename = system.io.path.getfilename(filepath);
4 ] @0 r8 I t8 W. v. B2 H
try
T8 [) Q* Q2 H+ q4 {
{
6 F# N# I! V; u7 b7 ?' \
// open the file.
7 z3 P5 @+ d7 J6 D) g0 @
istream = new system.io.filestream(filepath, system.io.filemode.open,
+ a" d, g, _' R& b7 N/ H, P
system.io.fileaccess.read, system.io.fileshare.read);
# L; v: }6 s7 S" t' G! Q* c
// total bytes to read.
! C5 W% h" s6 t8 `
datatoread = istream.length;
8 ~ k e1 n+ Q
response.clear();
$ @9 g+ j" H3 o, P# s% N1 \$ x: l, C
response.clearheaders();
% z5 f3 n8 t" Q0 `: N' E( k7 y
response.clearcontent();
. z) H' a( \) S9 q
response.contenttype = text/plain; // set the file type
l+ Z) r% N) Q5 B" c, y
response.addheader(content-length, datatoread.tostring());
$ p" M& B' d) F" Q
response.addheader(content-disposition, attachment; filename= + filename);
! h' A( }3 }# v
// read the bytes.
1 t, F; G0 K6 I0 H5 F: S
while (datatoread > 0)
3 }$ k% ~* K) @. W
{
1 }, r+ m) F# h! b0 v
// verify that the client is connected.
4 {7 ]9 p( T% q: y$ D0 ~( i
if (response.isclientconnected)
, a. p$ k; T6 Y
{
' B+ }6 O- }- f% Q0 ?
// read the data in buffer.
, F9 f+ J" O2 D* j: [6 P+ P3 a
length = istream.read(buffer, 0, 10000);
. {! t: j4 R. O/ l
// write the data to the current output stream.
1 ^0 M) ]% R2 E6 W+ N
response.outputstream.write(buffer, 0, length);
, L" Q! r' @4 e: Q
// flush the data to the html output.
3 v1 \, d, O: T3 t6 K4 }' H
response.flush();
( k z v* g6 H/ H) G7 i
buffer = new byte[10000];
; U' `# M7 W( C q# o$ R3 K- j
datatoread = datatoread - length;
2 k% ^8 |0 V) o
}
. a7 v7 i) k( ]! _* }4 P
else
' \' Y! d& w: J3 ~
{
+ l* u! x2 \9 l3 R9 S: p! s+ t
// prevent infinite loop if user disconnects
! V% `3 O+ U0 y
datatoread = -1;
) a2 V+ H3 s7 ^8 J
}
. k9 T, I7 `6 F/ p3 `
}
: t" O, B2 } J! `2 J1 q
}
# g/ r4 L( H# i0 g8 t$ N2 f! s- e
catch (exception ex)
% M6 V" w6 [; U$ V6 \
{
' j7 h$ |9 Y- F7 e5 k/ J
// trap the error, if any.
. B& _( s! i. R: y ^
response.write(error : + ex.message);
) N* [$ l S, X" N; e' I
}
* b7 C! ~ S9 F- a1 m
finally
) C" e& Q* x; K( G+ n$ _' `* w. S
{
& ^3 `& {3 ]% G& X, ?
if (istream != null)
1 u* g- s7 N) P8 g% Z8 H: H
{
+ m5 [& S) @( ~, |# ]
//close the file.
6 V- `+ x( P7 _2 v9 a' f. k1 u
istream.close();
' {1 D, f( `( z4 ?
}
! ~3 M) F# Q1 X& x
response.end();
T( x; h# y0 O [
}
4 s! z1 f2 O; w6 @- u( ^
}
, _2 d0 @( z. n. H/ R( Y1 O, W
}
3 q. ` o0 A& r, n* f
}
$ a. p$ I9 L8 m0 [% u
希望本文所述对大家的asp.net程序设计有所帮助。
6 ?" O8 {2 P" R; p3 d
f+ |) O$ ^/ n/ f5 w0 g
更多网页制作信息请查看: 网页制作
作者:
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