码迷,mamicode.com
首页 > 其他好文 > 详细

流方式下载文件

时间:2019-11-04 17:22:48      阅读:50      评论:0      收藏:0      [点我收藏+]

标签:efault   cte   信息   chm   isp   action   错误信息   byte   osi   

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("附件不存在!");
}
}

流方式下载文件

标签:efault   cte   信息   chm   isp   action   错误信息   byte   osi   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!