流方式下载文件

public ActionResult DownLoadAttachment(string attachmentId)
{
Attachment attachment = _customerInfoService.GetAttachment(attachmentId);
if (attachment != null)
{
try
{
string fileName = attachment.FileName;
string filePath = attachment.Path;
string fileType = attachment.FileType;
#region 流方式下载文件
Encoding encoding;
string outputFileName = null;
fileName = fileName.Replace("‘", "");

string browser = Request.UserAgent.ToUpper();
if (browser.Contains("MS") == true && browser.Contains("IE") == true)
{
outputFileName = HttpUtility.UrlEncode(fileName);
encoding = Encoding.Default;
}
else if (browser.Contains("FIREFOX") == true)
{
outputFileName = fileName;
encoding = Encoding.GetEncoding("GB2312");
}
else
{
outputFileName = HttpUtility.UrlEncode(fileName);
encoding = Encoding.Default;
}
FileStream fs = new FileStream(filePath, FileMode.Open);
long length = fs.Length;
byte[] bytes = new byte[(int)fs.Length];
fs.Read(bytes, 0, bytes.Length);
fs.Close();
Response.Charset = "UTF-8";
Response.ContentType = "application/octet-stream";
Response.ContentEncoding = encoding;
Response.AddHeader("Content-Disposition", "attachment; filename=" + outputFileName);
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
return new EmptyResult();
#endregion
}
catch (Exception ex)
{
return Content(string.Format("附件下载失败!错误信息:{0}", ex.Message));
}

}
else
{
return Content("附件不存在!");
}
}

原文地址:https://www.cnblogs.com/zhangzhang1118/p/11792831.html

时间: 2024-08-30 18:06:52

流方式下载文件的相关文章

使用一般处理程序HTTPHandler下载文件

一般来说我们可以用HTTPHandler来处理一些简单的逻辑,比如验证码.下载文件等. 以下载word文档为例讲解一下如何在HHTPHandler中下载文件,不限于word文档,如果下载其他文件,需要注意的是要将“ context.Response.ContentType = "application/msword"; ”设置为其他相应格式或通用格式“application/octet-stream”,来看代码 1 //文件名 2 const string fileName = &qu

c# 浏览器弹出提示框下载文件

c#下载文件四种方法: 1. 流方式下载 2.TransmitFile实现下载 3.WriteFile实现下载 4.WriteFile分块下载 /// <summary> /// 流方式下载文件不能超过400M /// </summary> /// <param name="filePath">服务器相对路径</param> public void RenderToBrowser(string filePath) { filePath =

数据表格,查询、导出共用一个form表单,实现文件流方式下载

在开发中遇到问题是这样的: 在维护老的管理系统的过程中,老板说让加导出功能:项目中,查询的筛选条件是用的表单提交的方式写的. 解决方案有两种: 一.用ajax方式导出 var array = $('#frmSearch').serialize(); 获得表单数据后,用post方式提交给服务器,服务器返回文件所存在的网络地址,然后用windows.open()的方式下载文件 但是我希望文件下载后,能够把文件删除了:用上边方式就不太合适了,不能及时删除旧文件,于是想出下面的方式: 二.文件流的方式下

asp.net下载文件几种方式

protected void Button1_Click(object sender, EventArgs e)  {  /*  微软为Response对象提供了一个新的方法TransmitFile来解决使用Response.BinaryWrite  下载超过400mb的文件时导致Aspnet_wp.exe进程回收而无法成功下载的问题.  代码如下:  */ Response.ContentType = "application/x-zip-compressed";  Response

文件流的方式下载文件

//流方式下载 protected void Button4_Click(object sender, EventArgs e) { string fileName = "aaa.zip";//客户端保存的文件名 string filePath = Server.MapPath("DownLoad/aaa.zip");//路径 //以字符流的形式下载文件 FileStream fs = new FileStream(filePath, FileMode.Open);

asp.net C#实现下载文件的六种方法实例

protected void Button1_Click(object sender, EventArgs e)  {  /*  微软为Response对象提供了一个新的方法TransmitFile来解决使用Response.BinaryWrite  下载超过400mb的文件时导致Aspnet_wp.exe进程回收而无法成功下载的问题.  代码如下:  */ Response.ContentType = "application/x-zip-compressed";  Response

C# 从服务器下载文件代码

一.//TransmitFile实现下载 protected void Button1_Click(object sender, EventArgs e) { /* 微软为Response对象提供了一个新的方法TransmitFile来解决使用Response.BinaryWrite 下载超过400mb的文件时导致Aspnet_wp.exe进程回收而无法成功下载的问题. 代码如下: */ Response.ContentType = "application/x-zip-compressed&q

.net 下载文件几种方式

方式一:TransmitFile实现下载.将指定的文件直接写入 HTTP 响应输出流,而不在内存中缓冲该文件. protected void Button1_Click(object sender, EventArgs e) ...{ /**//* 微软为Response对象提供了一个新的方法TransmitFile来解决使用Response.BinaryWrite下载超过400mb的文件时导致Aspnet_wp.exe进程回收而无法成功下载的问题. 代码如下: */ Response.Cont

Asp.Net 下载文件的几种方式

asp.net下载文件几种方式 protected void Button1_Click(object sender, EventArgs e) { /* 微软为Response对象提供了一个新的方法TransmitFile来解决使用Response.BinaryWrite 下载超过400mb的文件时导致Aspnet_wp.exe进程回收而无法成功下载的问题. 代码如下: */ Response.ContentType = "application/x-zip-compressed";