asp.net下载文件 - accphy的专栏 - CSDNBlog
来源:互联网 发布:怎么求根号的值 java 编辑:程序博客网 时间:2024/05/14 18:31
//调用下载文件的方法
Session["file_txt"] = Request.QueryString["file_name"].ToString();//文件名字
string file_name = Session["file_txt"].ToString();//文件名字
bool success = ResponseFile(Page.Request, Page.Response, file_name, Request.MapPath("../newadmin/upfiles/file_xz/" + file_name), 1024000);
if(!success)
Response.Write("下载文件出错!");
Page.Response.End();
}
///
/// _fileName: 下载文件名, _fullPath: 带文件名下载路径, _speed 每秒允许下载的字节数返回是否成功
///
/// Page.Request对象
/// Page.Response对象
/// 下载文件名
/// 带文件名下载路径
///
///
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)(1000 * pack / _speed) + 1;
//int sleep =(int)System.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)((fileLength - startBytes) / pack) + 1;
//int maxCount =(int)System.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(Exception ex)
{
throw (ex);
}
- asp.net下载文件 - accphy的专栏 - CSDNBlog
- ASP.NET极速DataReader分页程序 - Gerry的专栏 - CSDNBlog
- ASP.NET MVC + jQuery + Newtonsoft.Json 快樂的AJAX - lee576的专栏 - CSDNBlog
- Asp.Net2.0(VB.Net)Access数据库连接类 - mylovepzq的专栏 - CSDNBlog
- Asp.net+Xml实现无数据库论坛一点即通 - lshdic的专栏 - CSDNBlog
- Java动态显示文件上传进度的简单实现 - java2000.net的专栏 - CSDNBlog
- access_flags - raphaelxia的专栏 - CSDNBlog
- 框架 - Pascal的专栏 - CSDNBlog
- ASP.NET 文件的下载
- 最新最全的ASP.NET学习资源大全(2008年03月01日更新) - 雪尘的专栏 - CSDNBlog
- 每个开发人员现在应该下载的十种必备工具(转载) - Larry的专栏 - CSDNBlog
- 2007必须学习的10项.NET技术 - qzgqzg198077的专栏 - CSDNBlog
- 一位软件工程师的6年总结 - java2000.net的专栏 - CSDNBlog
- .NET生成静态页面并分页 - 死亡凋零的专栏 - CSDNBlog
- 深度解析:J2EE vs .NET开发平台 - java060515的专栏 - CSDNBlog
- 什么是COM组件 - liming0658的专栏 - CSDNBlog
- SQLServer2000数据类型 - jiangxianfu的专栏 - CSDNBlog
- 什么是vml - chenyq2008的专栏 - CSDNBlog
- JNI 简单使用小结
- c#中using的几种用法
- 如何禁止删除某个记录?
- ASP.NET中下载文件
- Hibernate中DetachedCriteria的使用
- asp.net下载文件 - accphy的专栏 - CSDNBlog
- Hibernate Criteria 关联查询
- asp.net提交到其他页例程
- 转 master to master replication
- 不易注意的.net中using的几种用途
- 金价波动
- 一些好的代码,收集
- 昆明创业资料
- 30个适合女生玩的可爱网站