mvc 文件下载
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 文件下载的更多相关文章
- .net Mvc文件下载的功能,大文件下载完成之后修改数据库功能
原文:.net Mvc文件下载的功能,大文件下载完成之后修改数据库功能 我服务器上文件只能下载一次,下载了之后就不能下载了,大文件或网速不好时,可能服务端文件流发送完了,客户端还没下载完,导致下载失败 ...
- ASP.net MVC 文件下载的几种方法
ASP.net MVC 文件下载的几种方法(欢迎讨论) 在ASP.net MVC 中有几种下载文件的方法前提:要下载的文件必须是在服务器目录中的,至于不在web项目server目录中的文件下载我不 ...
- ASP.net MVC 文件下载的几种方法(欢迎讨论)
在ASP.net MVC 中有几种下载文件的方法 前提:要下载的文件必须是在服务器目录中的,至于不在web项目server目录中的文件下载我不知道,但是还挺想了解的. 第一种:最简单的超链接方法,&l ...
- Spring4 MVC文件下载实例
这篇文章将向您展示如何使用Spring MVC4执行文件下载,我们将看到应用程序从文件系统内部以及外部文件下载文件. 本教程的主要亮点: 下载文件是相当简单的,涉及以下步骤. 创建一个InputStr ...
- Spring MVC文件下载
方案一: // 文件下载 @RequestMapping(value = "/downloadFile") public ResponseEntity<byte[]> ...
- 【基础】ASP.net MVC 文件下载的几种方法(欢迎讨论)
在ASP.net MVC 中有几种下载文件的方法 前提:要下载的文件必须是在服务器目录中的,至于不在web项目server目录中的文件下载我不知道,但是还挺想了解的. 第一种:最简单的超链接方法,&l ...
- Spring4 MVC文件下载实例(javaconfig)
展示如何使用Spring MVC4执行文件下载,我们将看到应用程序从文件系统内部以及外部文件下载文件. 下载文件是相当简单的,涉及以下步骤. 创建一个InputStream到文件用于下载. 查找MIM ...
- spring mvc 文件下载 get请求解决中文乱码问题
方案简写,自己或有些基础的可以看懂,因为没时间写的那么详细 方案1 spring mvc解决get请求中文乱码问题, 在tamcat中server.xml文件 URIEncoding="UT ...
- MVC文件下载和webform也能使用的下载方法
public ActionResult Index() { DownloadMethod("text/plain", "C:/Users/sunny/Pictures/S ...
- spring mvc 文件下载
在controller中进行代码编写: @RequestMapping("/download") public ResponseEntity<byte[]> downl ...
随机推荐
- 无法序列化会话状态。在“StateServer”或“SQLServer”模式下,ASP.NET 将序列化会话状态对象,因此不允许使用无法序列化的对象或 MarshalByRef 对象。如果自定义会话状态存储在“Custom”模式下执行了类似的序列化,则适用同样的限制。
将项目部署到服务器后发现有如下问题,查了网上好多说是需要被序列化的类没有写上[Serializable]标志,所以把全部需要序列化的列都写上了标志发现还是不是,最后查到了发现网上说的并不太准确,而是需 ...
- iOS 上传自己的库到cocoapod
最近自己写了个库,传到github上,想让自己的库支持cocoapod,这里我看了很多相关文章.下面我就写下详细步骤以及会遇到的问题. 我们会使用trunk的方式提交到cocoa pod 这是2014 ...
- Selenium基础知识(五)多窗口切换
说到多窗口切换必须想到driver.switch_to.window()方法 driver.switch_to.window() 实现在不同窗口之间切换 driver.current_window_h ...
- opencv 傅里叶使用
#include<opencv2/opencv.hpp>#include<iostream>using namespace std;using namespace cv;int ...
- teragen/terasort_简化版
1, 关闭Hadoop安全模式 进入hdfs用户 su – hdfs Cd /opt/cloudera/parcels/CDH-5.12.1-1.cdh5.12.1.p0.3/bin hdfs dfs ...
- 011-Server服务器对象属性
Transfer:第一个页面直接调用第二个页面,执行完第二个页面后不再返回第一个页面,立即响应到客户端浏览器.Execute:第一个页面直接调用第二个页面,执行完第二个页面后再返回第一个页面执行,最后 ...
- c3p0:Connections could not be acquired from the underlying database!解决方案
在利用ssh框架做网站的时候遇到了一个比较棘手的问题,一直连接不上数据库,问题描述如下: 各种百度然后说的最多的解决方案是: 1,驱动配置有误:2,数据库连接地址有误:3,密码或帐号有误: 4,数据库 ...
- python自定义安装包
python的第三方模块越来越丰富,涉及的领域也非常广,如科学计算.图片处理.web应用.GUI开发等.当然也可以将自己写的模块进行打包或发布.一简单的方法是将你的类包直接copy到python的li ...
- Day10 Python网络编程 Socket编程
一.客户端/服务器架构 1.C/S架构,包括: 1.硬件C/S架构(打印机) 2.软件C/S架构(web服务)[QQ,SSH,MySQL,FTP] 2.C/S架构与socket的关系: 我们学习soc ...
- 每天记命令:lscpu 和 cat /proc/cpuinfo
[1]lscpu lscpu命令,查看cpu相关的统计信息. socket 就是主板上插cpu的槽的数目,也就是可以插入的物理CPU的个数(比如上例,可以插入1个CPU). core 就是我们平时说的 ...