All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
使用PHP程序来保护你的flash作品_[PHP教程]
c#编程中学习数据结构的必要性
C#实现Word中表格信息读取_[Asp.Net教程]
ASP.NET页面刷新的几种方法_[Asp.Net教程]
轻松掌握Ajax.net系列教程十六:使用DropDownExtender_[Asp.Net教程]
利用XMLHTTP无刷新自动实时更新数据_[XML教程]
ASP.NET生成静态页面实现方法_[Asp.Net教程]
程序开发:ASP.NET热点问题解答14个_.net资料_编程技术
HTML语言剖析(十二)多媒体标记_[Html教程]
Visual C++ 6.0教程:函数的定义与调用
Reading number is top 10 pictures
Born After 90 Beijing sports university campus flower photos5
Extremely rare TianShan Mountains snow lotus1
Sell the barbecue as says father du breul1
9.3阅兵全景图6-常规导弹梯队和核导弹梯队
Sora aoi on twitter5
Born After 90 Beijing sports university campus flower photos4
Average female college students2
到南昌西站了3
Take you to walk into the most true north Korea rural3
湖南中医院的养生八图
Download software ranking
WebService在.NET中的实战应用教学视频 → 第1集
天龙八部十二宫服务端
Love the forty days
网页特效实例大全
jdk1.5
卡丁车单机版
Jinling thirteen stock
I for your crazy
Proficient in Eclipse
Tram sex maniac 2 (H) rar bag10
delv published in(发表于) 2014/1/24 9:08:03 Edit(编辑)
ASP.Net文件下载函数_[Asp.Net教程]

ASP.Net文件下载函数_[Asp.Net教程]

ASP.Net文件下载函数_[Asp.Net教程]

ASP.net文件下载函数
using System;
using System.IO;
using System.Threading;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
//在此处调用
Page.Response.Clear();

bool success = ResponseFile(Page.Request, Page.Response, "picture.rar", Request.MapPath("../../ReportTemplate/ExcelTemplates/picture.rar"), 1024000);

if(!success)
Response.Write("下载文件出错!");
Page.Response.End();
break;
}


// 输入参数 _Request: Page.Request对象, _Response: Page.Response对象, _fileName: 下载文件名, _fullPath: 带文件名下载路径, _speed 每秒允许下载的字节数
// 返回是否成功
public static bool ResponseFile(HttpRequest _Request,HttpResponse _Response,string _fileName,string _fullPath, long _speed)
{
try
{
FileStream myFile = new FileStream(_fullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
BinaryReader br = new BinaryReader(myFile);
try
{
_Response.AddHeader("Accept-Ranges", "bytes");
_Response.Buffer = false;
long fileLength = myFile.Length;
long startBytes = 0;

int pack = 10240; //10K bytes
//int sleep = 200; //每秒5次 即5*10K bytes每秒
int sleep = (int)Math.Floor(1000 * pack / _speed) + 1;
if (_Request.Headers["Range"] != null)
{
_Response.StatusCode = 206;
string[] range = _Request.Headers["Range"].Split(new char[] {’=’, ’-’});
startBytes = Convert.ToInt64(range[1]);
}
_Response.AddHeader("Content-Length", (fileLength - startBytes).ToString());
if (startBytes != 0)
{
_Response.AddHeader("Content-Range", string.Format(" bytes {0}-{1}/{2}", startBytes, fileLength-1, fileLength));
}
_Response.AddHeader("Connection", "Keep-Alive");
_Response.ContentType = "application/octet-stream";
_Response.AddHeader("Content-Disposition","attachment;filename=" + HttpUtility.UrlEncode(_fileName,System.Text.Encoding.UTF8) );

br.BaseStream.Seek(startBytes, SeekOrigin.Begin);
int maxCount = (int) Math.Floor((fileLength - startBytes) / pack) + 1;
for (int i = 0; i < maxCount; i++)
{
if (_Response.IsClientConnected)
{
_Response.BinaryWrite(br.ReadBytes(pack));
Thread.Sleep(sleep);
}
else
{
i=maxCount;
}
}
}
catch
{
return false;
}
finally
{
br.Close();
myFile.Close();
}
}
catch
{
return false;
}
return true;
}







添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.