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