得知互动
标题:
asp.net下载大文件的实现方法
[打印本页]
作者:
swmozowtfl
时间:
2015-7-31 22:04
标题:
asp.net下载大文件的实现方法
本文实例讲述了asp.net下载大文件的实现方法。分享给大家供大家参考。具体分析如下:
$ V* B) H& N3 ~5 b8 b+ S f
当我们的网站需要支持下载大文件时,如果不做控制可能会导致用户在访问下载页面时发生无响应,使得浏览器崩溃。可以参考如下代码来避免这个问题。
5 {6 Z, M. ]1 O' `, y' A
关于此代码的几点说明:
( m' z# E! g- \6 D; U' W! _2 w
1. 将数据分成较小的部分,然后将其移动到输出流以供下载,从而获取这些数据。
% G2 m1 g: h2 q* K8 a# N8 r
2. 根据下载的文件类型来指定 response.contenttype 。(参考oschina的这个网址可以找到大部分文件类型的对照表:)
! f! c1 U; K: E# [7 s$ F
3. 在每次写完response时记得调用 response.flush()
7 x' r! c+ J* r" G. M8 Y% [
4. 在循环下载的过程中使用 response.isclientconnected 这个判断可以帮助程序尽早发现连接是否正常。若不正常,可以及早的放弃下载,以释放所占用的服务器资源。
5 U6 R2 {3 B* t2 r, o: l. F5 n
5. 在下载结束后,需要调用 response.end() 来保证当前线程可以在最后被终止掉。
8 n% y# |: c( i' P3 m& B
代码如下:
5 I6 N+ E; D$ e. z- x u- W8 E+ P
using system;
P% d {# I6 ^. U1 ?1 R
namespace webapplication1
2 N. z2 _+ ?9 z7 n
{
( ]" B% P) D* z) j% z- X
public partial class downloadfile : system.web.ui.page
; G) u- |: Z6 R R5 L9 F) [6 S
{
' L4 H& Q2 U& G( ^
protected void page_load(object sender, eventargs e)
5 D0 d" z4 g8 y) |
{
+ d' g1 O: h: }* `
system.io.stream istream = null;
& c& G* @7 L) y5 g; u0 D- B% r
// buffer to read 10k bytes in chunk:
: \- r/ @6 N% k7 v- m8 M. p
byte[] buffer = new byte[10000];
1 R4 b9 ?2 [0 Z$ K( t! s* H
// length of the file:
, i w% i b* n5 q6 H
int length;
$ D, k% p& r& f* J* f& E- K6 w+ k
// total bytes to read.
5 j* O& p) N0 j) d5 j; z7 F7 K/ E3 H
long datatoread;
/ i3 E* B2 M8 g; `
// identify the file to download including its path.
. Z- s! N; i% m+ K
string filepath = server.mappath(/) +./files/textfile1.txt;
* J f G7 O9 s+ h- i! b
// identify the file name.
2 b: `4 o: X: l/ m, t; L$ y' c
string filename = system.io.path.getfilename(filepath);
. a, d. T8 j! p
try
' x( |. R" b* r2 y! \
{
3 i2 k) ^. E: ]( i0 Q6 o$ Y
// open the file.
! q0 \! u9 R: s* z1 b6 S
istream = new system.io.filestream(filepath, system.io.filemode.open,
& z8 f2 i( [& F8 Z- n6 `% K
system.io.fileaccess.read, system.io.fileshare.read);
# `4 p9 `/ ~" i/ `: G7 U! B- V
// total bytes to read.
( o( l6 {( \ Q/ c# m# f
datatoread = istream.length;
+ ]9 q1 K5 M3 B
response.clear();
$ ]% Q9 M3 `+ l, u `1 ~
response.clearheaders();
: B; Q0 ~% e. y
response.clearcontent();
! I# v. J8 e4 j
response.contenttype = text/plain; // set the file type
4 X/ Q2 V* z8 F: s6 {
response.addheader(content-length, datatoread.tostring());
7 p7 {2 @* B# s' [
response.addheader(content-disposition, attachment; filename= + filename);
1 H0 V+ t' Y% J" E5 R" t
// read the bytes.
+ L1 N3 ^9 U9 w) c2 f: a# l- [* r% k
while (datatoread > 0)
; {# @7 k! ?- Q7 I9 P
{
9 v) p2 U9 d9 P
// verify that the client is connected.
7 G& ? F7 i4 p; ^
if (response.isclientconnected)
1 V* c( x u$ I. Q0 l* x" ^6 `
{
' d5 z( ^ v* U0 ^7 Y( R+ T
// read the data in buffer.
. ~! ~: |0 j& W4 W9 V
length = istream.read(buffer, 0, 10000);
; Y; i4 J7 `$ s1 ^
// write the data to the current output stream.
% |' j. X& r4 i: B) [: Y
response.outputstream.write(buffer, 0, length);
2 e: e0 |' @+ L0 X+ J7 A9 X
// flush the data to the html output.
5 C2 p6 D' y6 C
response.flush();
4 c3 @7 r' y9 K+ s! M- A( p
buffer = new byte[10000];
9 B0 X l. r5 a) A* D9 q% O! l
datatoread = datatoread - length;
}# F$ y3 N7 @! c
}
+ Q9 h9 }+ M2 z: O( r/ B
else
W2 p- K9 w1 | P1 A1 `% `% |
{
. p w: D4 d( @! _6 P, B% L7 x
// prevent infinite loop if user disconnects
, X! I' p5 ]" K' ?
datatoread = -1;
% o I# W" k# M
}
6 B2 x! r# y& w
}
/ K h+ k7 `6 d% S
}
- t' W6 r/ W3 f( ?; S7 ]
catch (exception ex)
8 p# E0 l8 Z7 Z' F+ u
{
% c3 ^/ }2 P9 P' Q, L4 P
// trap the error, if any.
# n. j) ?5 Q$ j
response.write(error : + ex.message);
, j7 o. J' F! X7 y% w
}
! T. i* h! g. b
finally
, ^, O, H2 f* J. d: Z
{
8 b/ O( h: {. C8 _ k" M
if (istream != null)
& }& e, v: d8 U# m4 B
{
$ l0 O0 K$ @1 Y- X# b! g# V. s
//close the file.
3 n4 P0 W9 F+ X% A2 Z' E4 z
istream.close();
, y0 f9 m; g& X O# G
}
% h0 F. Y+ j3 u7 H% f
response.end();
, O, w. H \+ y5 X& w
}
Z$ R3 \! n2 A
}
, v% o1 f; ` G
}
7 t0 e$ s5 N& u
}
- m+ O; N) k+ ]6 Q! p {
希望本文所述对大家的asp.net程序设计有所帮助。
+ a# f% y0 R3 }9 p
2 X& X& }% E s8 ~& y9 g4 a( U' \
更多网页制作信息请查看: 网页制作
作者:
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