using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Threading; namespace ProjectWenDangManage.Framework
{
/// <summary>
/// HTTP文件下载辅助类
/// </summary>
public class HttpDownLoadHelper
{
/// <summary>
/// 文件下载
/// </summary>
/// <param name="_Request"></param>
/// <param name="_Response"></param>
/// <param name="_fileName">下载文件时的短文件名称</param>
/// <param name="_fullPath">待下载文件的绝对路径</param>
/// <param name="_speed">下载速度</param>
/// <returns></returns>
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 = ; double pack = ; //10K bytes
//int sleep = 200; //每秒5次 即5*10K bytes每秒
int sleep = (int)Math.Floor( * pack / _speed) + ;
if (_Request.Headers["Range"] != null)
{
_Response.StatusCode = ;
string[] range = _Request.Headers["Range"].Split(new char[] { '=', '-' });
startBytes = Convert.ToInt64(range[]);
}
_Response.AddHeader("Content-Length", (fileLength - startBytes).ToString());
if (startBytes != )
{
//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) + ; for (int i = ; i < maxCount; i++)
{
if (_Response.IsClientConnected)
{
_Response.BinaryWrite(br.ReadBytes(int.Parse(pack.ToString())));
Thread.Sleep(sleep);
}
else
{
i = maxCount;
}
}
return true;
}
catch
{
return false;
}
finally
{
br.Close(); myFile.Close();
}
}
catch
{
return false;
}
}
}
}

HttpDownLoadHelper

在webfrom中

string filePath=Path.Combine(HttpRuntime.AppDomainAppPath,"Files","项目管理工具.msi");
HttpDownLoadHelper.ResponseFile(Request, Response, "下载显示的名称", filePath, );

在mvc中

string filePath=Path.Combine(HttpRuntime.AppDomainAppPath,"Files","项目管理工具.msi");
HttpDownLoadHelper.ResponseFile(HttpContext.ApplicationInstance.Context.Request, HttpContext.ApplicationInstance.Context.Response, "下载显示的名称", filePath, );

最新文章

  1. 7-11使用UNION合并查询
  2. 客户端配置文件tnsname.ora
  3. Eclipse 导入项目后启动报异常:java.lang.UnsatisfiedLinkError: Native Library *.dll already loaded in another classloade 解决方法
  4. VMware打卡虚拟机提示“此虚拟机可能已被复制或移动”
  5. 深入理解JVM—性能监控工具
  6. Java学习-016-CSV 文件读取实例源代码
  7. Ionic基础——侧边栏ion-side-menus 以及ion-tap结合侧边栏详解
  8. 栈的应用-四则表达式(C#代码实现)
  9. 【java】Servlet 工程 web.xml 中的 servlet 和 servlet-mapping 标签
  10. BZOJ 3401: [Usaco2009 Mar]Look Up 仰望( 单调栈 )
  11. android mediaplayer
  12. Python之路: 面向对象
  13. Android源码编译jar包BUILD_JAVA_LIBRARY 与BUILD_STATIC_JAVA_LIBRARY的区别(三)
  14. Python基础-week04
  15. ext window嵌jsp页面自适应
  16. Django DetailView 多重继承 关系整理
  17. Java Spring Boot VS .NetCore (四)数据库操作 Spring Data JPA vs EFCore
  18. GIT命令介绍
  19. bzoj4821-线段树区间lazy_tag下放的优先级和区间覆盖
  20. Eclipse的使用与Oblect类的常用方法_DAY11

热门文章

  1. Android开发学习之路-自定义ListView(继承BaseAdapter)
  2. javascript中this指向
  3. IIS集成模式下,URL重写后获取不到Session值
  4. 【WP 8.1开发】如何处理摄像头翻转的问题
  5. Shader LOD
  6. 利用libpcap打印ip包
  7. run命令
  8. nodejs字符与字节之间的转换
  9. CentOS安装运行NodeJS框架Express
  10. PHP的学习--PHP的引用