ASP.NET MVC 下载文件

如何从 MVC 控制器(.NET Framework)下载文件。

使用 从 ASP.NET MVC 中的控制器下载任何文件类型FileStreamResult。注意:如果使用ASP.NET Core,请参阅此页面,如果想要将文件上传到服务器,请参阅此页面

// download a zip file as an attachment

public FileStreamResult DownloadZipFile()

{

string path = Server.MapPath("/files/somefile.zip");

if (System.IO.File.Exists(path))

{

Response.AppendHeader("content-disposition", "attachment;filename=somefile.zip");

System.IO.FileStream stream = new System.IO.FileStream(path, System.IO.FileMode.Open); // don't use using keyword!

return new FileStreamResult(stream, "application/x-zip-compressed"); // the constructor will fire Dispose() when done

}

else

return null;

}

// download a jpeg as an attachment

public FileStreamResult DownloadJpegFile()

{

string path = Server.MapPath("/files/someimage.jpg");

if (System.IO.File.Exists(path))

{

Response.AppendHeader("content-disposition", "attachment;filename=someimage.jpg");

System.IO.FileStream stream = new System.IO.FileStream(path, System.IO.FileMode.Open); // don't use using keyword!

return new FileStreamResult(stream, "image/jpeg"); // the constructor will fire Dispose() when done

}

else

return null;

}

// download a text file as an attachment

public FileStreamResult DownloadTextFile()

{

string path = Server.MapPath("/files/somefile.txt");

if (System.IO.File.Exists(path))

{

Response.AppendHeader("content-disposition", "attachment;filename=somefile.txt");

System.IO.FileStream stream = new System.IO.FileStream(path, System.IO.FileMode.Open); // don't use using keyword!

return new FileStreamResult(stream, "text/plain"); // the constructor will fire Dispose() when done

}

else

return null;

}

FileContentResult从使用字节数组的 ASP.NET MVC 控制器下载任何数据类型。

public FileContentResult DownloadTextFile()

{

byte[] bytes = System.Text.Encoding.ASCII.GetBytes("How now brown cow.");

Response.AppendHeader("content-disposition", "attachment;filename=sometext.txt");

return new FileContentResult(bytes, "text/plain");

}

或者使用FileStream来填充字节数组。

public FileContentResult DownloadTextFile()

{

System.IO.FileStream stream = new System.IO.FileStream("somefile.txt", System.IO.FileMode.Open);

byte[] bytes = new byte[stream.Length];

stream.Read(bytes, 0, bytes.Length);

Response.AppendHeader("content-disposition", "attachment;filename=somefile.txt");

return new FileContentResult(bytes, "text/plain");

}

或者简单来说:

public FileContentResult DownloadTextFile()

{

byte[] bytes = System.IO.File.ReadAllBytes("somefile.txt");

Response.AppendHeader("content-disposition", "attachment;filename=somefile.txt");

return new FileContentResult(bytes, "text/plain");

}

下载自MemoryStream

public FileContentResult DownloadFromMemoryStream()

{

using(System.IO.MemoryStream ms = new System.IO.MemoryStream())

{

ms.WriteByte((byte)'a');

ms.WriteByte((byte)'b');

ms.WriteByte((byte)'c');

byte[] bytes = ms.ToArray();

Response.AppendHeader("content-disposition", "attachment;filename=somefile.txt");

return new FileContentResult(bytes, "text/plain");

}

}

如果您喜欢此文章,请收藏、点赞、评论,谢谢,祝您快乐每一天。

相关推荐
fouryears_234179 小时前
Spring,Spring Boot 和 Spring MVC 的关系以及区别
java·spring boot·spring·mvc
Jinkxs2 天前
Spring MVC 执行流程详解:一次请求经历了什么?
java·spring·mvc
海天胜景2 天前
ASP.NET Core Hosting Bundle
后端·asp.net
JosieBook3 天前
【开源】一款基于 .NET 和 Vue3 开源(Apache)的MES管理系统,您的新一代工厂管理助手!
开源·asp.net
王柏龙3 天前
aspnetcore Mvc配置选项中的ModelMetadataDetailsProviders
mvc·.netcore
即将进化成人机4 天前
Spring-----MVC配置和基本原理
java·spring·mvc
王柏龙4 天前
aspnetcore Mvc配置选项中的ModelBindingMessageProvider
mvc·.net
FreeBuf_4 天前
黄金旋律IAB组织利用暴露的ASP.NET机器密钥实施未授权访问
网络·后端·asp.net
神仙别闹7 天前
基于ASP.NET MVC+SQLite开发的一套(Web)图书管理系统
sqlite·asp.net·mvc
神仙别闹7 天前
基于ASP.NET+SQL Server实现(Web)企业进销存管理系统
前端·后端·asp.net