ASP.NET MVC文档下载

方式一:
public FileStreamResult DownFile(string filePath, string fileName)
{
      string absoluFilePath = Server.MapPath(System.Configuration.ConfigurationManager.AppSettings["AttachmentPath"] +      filePath);
       return File(new FileStream(absoluFilePath, FileMode.Open), "application/octet-stream", Server.UrlEncode(fileName));
}




方式二:
public ActionResult DownFile(string filePath, string fileName)
{
filePath = Server.MapPath(System.Configuration.ConfigurationManager.AppSettings["AttachmentPath"] + filePath);
FileStream fs = new FileStream(filePath, FileMode.Open);
byte[] bytes = new byte[(int)fs.Length];
fs.Read(bytes, 0, bytes.Length);
fs.Close();
Response.Charset = "UTF-8";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
Response.ContentType = "application/octet-stream";


Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(fileName));
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
return new EmptyResult();


}


 


View调用:


<a href="/Document/DownFile?filePath=@item.Value&fileName=@item.Key">下载</a> 

关键词:nbsp response filepath filename string bytes 下载 server new fs

相关推荐:

mvc文档上传与下载

.net 下载文档

C#实现文档下载的几种方式

ASP.NET MVC实现Excel文档的上传下载以及Excel数据的保存

C# 操作文档的一些常用方法

c#代码文档上传和下载

【转】文档下载之断点续传(客户端与服务端的实现)

asp.net mvc文档下载

C# Response 下载