asp.net大文件下载
一个可尝试的方案,使用二进制流下载,下载地址是诸如 这样的连接,在 down 页面进行权限判断,比如是否登录了,当前用户的点数是否满足 911199 对应的文件的下载点数 等等
二进制下载实例代码,支持特大文件下载
string downfilepath = @ "d:\opensuse-10.2-gm-dvd-i386.iso "; // test with 3.8gb, ok // server.mappath( "~/files/somefile.iso ");
system.io.fileinfo downfileinfo = new system.io.fileinfo(downfilepath);
if (!downfileinfo.exists) throw new exception( "文件不存在。 ");
const int chunk_size = 10000; // 指定块大小
byte[] buffer = new byte[chunk_size];
response.clear();
// fails to down the big file with both the following methods
// error: system.argumentoutofrangeexception: 大小参数必须介于零和最大的 int32 值之间。
// response.writefile(downfilepath);
// or
// response.transmitfile(downfilepath); // asp.net 2.0 supported
//
using (system.io.filestream istream = system.io.file.openread(downfilepath)) {
long datalengthtoread = istream.length;
response.contenttype = "application/octet-stream ";
response.addheader( "content-disposition ",
"attachment; filename= " + server.urlpathencode(downfileinfo.name)); // 对文件名进行编码
while (datalengthtoread > 0 && response.isclientconnected) {
int lengthread = istream.read(buffer, 0, chunk_size);
response.outputstream.write(buffer, 0, lengthread);
response.flush();
datalengthtoread = datalengthtoread - lengthread;
}
}
response.close();
作者 郑文亮