输出硬盘文件,提供下载
来源:互联网 发布:淘宝的数字暗语大全 编辑:程序博客网 时间:2024/04/30 09:36
///<summary>
/// 输出硬盘文件,提供下载 支持大文件、续传、速度限制、资源占用小
/// </summary>
/// <param name="MyRequest">Page.Request对象</param>
/// <param name="MyResponse">Page.Response对象</param>
/// <param name="MyFileName">下载文件名</param>
/// <param name="MyFullPath">带文件名下载路径</param>
/// <param name="MySpeed">每秒允许下载的字节数</param>
/// <returns>True Or False</returns>
public bool ResponseFile(HttpRequest MyRequest, HttpResponse MyResponse, string MyFileName, string MyFullPath, long MySpeed)
{
try
{
FileStream myFile = new FileStream(MyFullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
BinaryReader br = new BinaryReader(myFile);
try
{
MyResponse.AddHeader("Accept-Ranges", "bytes");
MyResponse.Buffer = false;
long fileLength = myFile.Length;
long startBytes = 0;
int pack = 10240; //10K bytes
int sleep = (int)Math.Floor(1000.0 * pack / MySpeed) + 1;
if (MyRequest.Headers["Range"] != null)
{
MyResponse.StatusCode = 206;
string[] range = MyRequest.Headers["Range"].Split(new char[] { '=', '-' });
startBytes = Convert.ToInt64(range[1]);
}
MyResponse.AddHeader("Content-Length", (fileLength - startBytes).ToString());
if ((startBytes != 0))
{
MyResponse.AddHeader("Content-Range", string.Format(" bytes {0}-{1}/{2}", startBytes, fileLength - 1, fileLength));
}
MyResponse.AddHeader("Connection", "Keep-Alive");
MyResponse.ContentType = "application/octet-stream";
MyResponse.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(MyFileName, System.Text.Encoding.UTF8));
br.BaseStream.Seek(startBytes, SeekOrigin.Begin);
int maxCount = (int)Math.Floor(1.0 * (fileLength - startBytes) / pack) + 1;
for (int i = 0; i <= maxCount; i++)
{
if ((MyResponse.IsClientConnected))
{
MyResponse.BinaryWrite(br.ReadBytes(pack));
System.Threading.Thread.Sleep(sleep);
}
else
{
i = maxCount;
}
}
}
catch
{
return false;
}
finally
{
br.Close(); myFile.Close();
}
}
catch
{
return false;
}
return true;
}
备注:未测试
========================
- 输出硬盘文件,提供下载
- 输出硬盘文件,提供下载 支持大文件、续传、速度限制、资源占用小
- 输出硬盘文件,提供下载 支持大文件、续传、速度限制、资源占用小
- 硬盘文件下载
- ASP.NET提供文件下载
- django服务器提供下载文件
- Servlet提供文件下载功能
- 使用HttpServletResponse提供文件下载
- 输出文件的下载
- java 输出到硬盘文件上
- ASP.NET提供文件下载函数
- ASP.NET提供文件下载函数
- ASP.NET提供文件下载函数(转载)
- ASP.NET提供文件下载函数
- ASP.NET提供文件下载函数
- ASP.NET提供文件下载函数
- java提供文件下载的方法
- ASP.NET提供文件下载函数
- 21.6 小结(征服ASP.NET 2.0 Ajax——Web开发技术详解 )
- Posix多线程编程学习笔记(二)—线程属性(1)
- test
- 创建定制的泛型类--泛型Queue多线程读写文档--的泛型方法
- 提高oracle表分析速度
- 输出硬盘文件,提供下载
- Posix多线程编程学习笔记(二)—线程属性(2)
- C64X DSP EDMA/QDMA小结
- PortHidDemo_Vista
- WinCVS 与 SourceForge.net 进行ssh连接
- GIS开源软件推荐-UDIG
- 编译原理资料
- 北京奥运会开幕式彩排 观众提前四小时等候
- Rustock.C