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");

}

}

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

相关推荐
咖丨喱8 小时前
【Action帧简要分析】
服务器·数据库·asp.net
发粪的屎壳郎1 天前
ASP.NET Core 8 轻松配置Serilog日志
后端·asp.net·serilog
别来无恙1491 天前
整合Spring、Spring MVC与MyBatis:构建高效Java Web应用
java·spring·mvc
程序员秘密基地2 天前
基于html,css,vue,vscode,vs2022,asp.net,aspnet,.net,c#,mysql数据库,在线健身,俱乐部管理系统
前端·vue.js·后端·mysql·asp.net
wstcl3 天前
让你的asp.net网站在调试模式下也能在局域网通过ip访问
后端·tcp/ip·asp.net
ajassi20003 天前
开源 C# .net mvc 开发(八)IIS Express轻量化Web服务器的配置和使用
linux·开源·c#·mvc·.net
合作小小程序员小小店3 天前
web网页开发,在线%ctf管理%系统,基于html,css,webform,asp.net mvc, sqlserver, mysql
mysql·sqlserver·性能优化·asp.net·mvc
Exclusive_Cat4 天前
SpringMVC参数接收与数据返回详解
spring·mvc
Penk是个码农5 天前
web前端面试-- MVC、MVP、MVVM 架构模式对比
前端·面试·mvc
专注VB编程开发20年7 天前
开机自动后台运行,在Windows服务中托管ASP.NET Core
windows·后端·asp.net