public class DownLoadHelper
{ /// <summary>
/// WriteFile实现下载--测试通过
/// </summary>
/// <param name="filePath">文件路径</param>
public static void DownLoadWithWriteFile(string filePath)
{
string fileName = Path.GetFileName(filePath);//客户端保存的文件名
FileInfo fileInfo = new FileInfo(filePath);
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
HttpContext.Current.Response.AddHeader("Content-Length", fileInfo.Length.ToString());
HttpContext.Current.Response.AddHeader("Content-Transfer-Encoding", "binary");
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
HttpContext.Current.Response.WriteFile(fileInfo.FullName);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
}
/// <summary>
/// WriteFile分块下载--测试通过
/// </summary>
/// <param name="filePath">文件路径</param>
public static void DownLoadWithWriteFileByStep(string filePath)
{
string fileName = Path.GetFileName(filePath);//客户端保存的文件名
System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);
if (fileInfo.Exists)
{
const long ChunkSize = ;//100K 每次读取文件,只读取100K,这样可以缓解服务器的压力
byte[] buffer = new byte[ChunkSize];
HttpContext.Current.Response.Clear();
System.IO.FileStream iStream = System.IO.File.OpenRead(filePath);
long dataLengthToRead = iStream.Length;//获取下载的文件总大小
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName));
while (dataLengthToRead > && HttpContext.Current.Response.IsClientConnected)
{
int lengthRead = iStream.Read(buffer, , Convert.ToInt32(ChunkSize));//读取的大小
HttpContext.Current.Response.OutputStream.Write(buffer, , lengthRead);
HttpContext.Current.Response.Flush();
dataLengthToRead = dataLengthToRead - lengthRead;
}
HttpContext.Current.Response.Close();
}
}
/// <summary>
/// 流方式下载--测试通过
/// </summary>
/// <param name="filePath">文件路径</param>
public static void DownLoadWithStream(string filePath)
{
string fileName = Path.GetFileName(filePath);//客户端保存的文件名
//以字符流的形式下载文件
FileStream fs = new FileStream(filePath, FileMode.Open);
byte[] bytes = new byte[(int)fs.Length];
fs.Read(bytes, , bytes.Length);
fs.Close();
HttpContext.Current.Response.ContentType = "application/octet-stream";
//通知浏览器下载文件而不是打开
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
HttpContext.Current.Response.BinaryWrite(bytes);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
}
/// <summary>
/// TransmitFile实现下载--测试通过
/// </summary>
/// <param name="filePath">文件路径</param>
public static void DownLoadWithTransmitFile(string filePath)
{
string fileName = Path.GetFileName(filePath);//客户端保存的文件名
/*
微软为Response对象提供了一个新的方法TransmitFile来解决使用Response.BinaryWrite
下载超过400mb的文件时导致Aspnet_wp.exe进程回收而无法成功下载的问题。
*/
HttpContext.Current.Response.ContentType = "application/x-zip-compressed";
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename="+ HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
HttpContext.Current.Response.TransmitFile(filePath);
}
}
调用方法:
public void DownMap(string LinkUrl)
{
string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "DownLoadSource\\1.txt");
DownLoadHelper.DownLoadWithTransmitFile(filePath);
}
前端:
function download(linkUrl) {
window.location.href = "/Home/DownMap?LinkUrl=" + linkUrl;
}

  

  

 

mvc 文件下载的更多相关文章

  1. .net Mvc文件下载的功能,大文件下载完成之后修改数据库功能

    原文:.net Mvc文件下载的功能,大文件下载完成之后修改数据库功能 我服务器上文件只能下载一次,下载了之后就不能下载了,大文件或网速不好时,可能服务端文件流发送完了,客户端还没下载完,导致下载失败 ...

  2. ASP.net MVC 文件下载的几种方法

      ASP.net MVC 文件下载的几种方法(欢迎讨论) 在ASP.net MVC 中有几种下载文件的方法前提:要下载的文件必须是在服务器目录中的,至于不在web项目server目录中的文件下载我不 ...

  3. ASP.net MVC 文件下载的几种方法(欢迎讨论)

    在ASP.net MVC 中有几种下载文件的方法 前提:要下载的文件必须是在服务器目录中的,至于不在web项目server目录中的文件下载我不知道,但是还挺想了解的. 第一种:最简单的超链接方法,&l ...

  4. Spring4 MVC文件下载实例

    这篇文章将向您展示如何使用Spring MVC4执行文件下载,我们将看到应用程序从文件系统内部以及外部文件下载文件. 本教程的主要亮点: 下载文件是相当简单的,涉及以下步骤. 创建一个InputStr ...

  5. Spring MVC文件下载

    方案一: // 文件下载 @RequestMapping(value = "/downloadFile") public ResponseEntity<byte[]> ...

  6. 【基础】ASP.net MVC 文件下载的几种方法(欢迎讨论)

    在ASP.net MVC 中有几种下载文件的方法 前提:要下载的文件必须是在服务器目录中的,至于不在web项目server目录中的文件下载我不知道,但是还挺想了解的. 第一种:最简单的超链接方法,&l ...

  7. Spring4 MVC文件下载实例(javaconfig)

    展示如何使用Spring MVC4执行文件下载,我们将看到应用程序从文件系统内部以及外部文件下载文件. 下载文件是相当简单的,涉及以下步骤. 创建一个InputStream到文件用于下载. 查找MIM ...

  8. spring mvc 文件下载 get请求解决中文乱码问题

    方案简写,自己或有些基础的可以看懂,因为没时间写的那么详细 方案1 spring mvc解决get请求中文乱码问题, 在tamcat中server.xml文件 URIEncoding="UT ...

  9. MVC文件下载和webform也能使用的下载方法

    public ActionResult Index() { DownloadMethod("text/plain", "C:/Users/sunny/Pictures/S ...

  10. spring mvc 文件下载

    在controller中进行代码编写: @RequestMapping("/download") public ResponseEntity<byte[]> downl ...

随机推荐

  1. 关于如何利用计算属性进行button的控制

    element分页没用它的 (这个只要上一页下一页),比如共2页的时候,你在第一页,你肯定可以点击下一页,当你进入到第二页的时候这个button肯定就不能点击了啊,它的属性diaabled=true让 ...

  2. XML反序列化遇到数字型节点值为空导致反序列化异常

    实体类: [XmlRoot("stream")] public class _30320DuisiFukuanQueryResponseModel : ResponseModelB ...

  3. 计数器控件实例 NumericStepper

    计数器控件实例 书:158 <?xml version="1.0" encoding="utf-8"?> <s:Application xml ...

  4. OBV15 案例2

    BV

  5. 关于Sublime Text3的emmet插件和tab快捷键冲突问题

    当使用Sublime text3时会遇到快捷键冲突的问题,其中就有安装Emmet之后,tab无法缩进了, 网上有些说看看Browse Packages目录下是否有PyV8插件安装,该插件一般情况下随E ...

  6. jQuery_ajax请求超时

    设置timeout的时间,通过检测complete时status的值判断请求是否超时,如果超时执行超时的操作. $.ajax({ url:'', timeout : 1000, //超时时间设置,单位 ...

  7. C# - 匿名对象取值

    在new出匿名对象的函数内可以直接调用该匿名对象的属性取值. 可是在其它函数就无法调用匿名对象的属性或方法. 这时,我们可以通过c#的反射机制取值: 文章出处:https://www.cnblogs. ...

  8. C#中换行的代码

    1.Windows 中的换行符"\r\n"2.Unix/Linux 平台换行符是 "\n".3.MessageBox.Show() 的换行符为 "\n ...

  9. PB中datewindow单双行显示不同颜色

    调出datewindow,找到detail中的列,右击properties,左侧Background中的color属性添加 IF(MOD(GETROW(),2)=0,RGB( 255, 250, 20 ...

  10. 四 js Math数学简单使用

    //Math是全局的 //Math.PI 数学里的3.1415926.... console.log(Math.PI); //取随机数 //js提供的随机函数 Math.random() --> ...