当前位置: 首页 > news >正文

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

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

相关文章:

  • Android移动应用开发实践-1-下载安装和简单使用Android Studio 3.5.2版本(频频出错)
  • Oracle中补全时间的处理
  • 计算机毕业设计SpringBoot+Vue.js教师工作量管理系统(源码+LW文档+PPT+讲解)
  • 本地部署AI模型 --- DeepSeek(二)---更新中
  • Keepalive基础
  • 具备智能广告拦截、个性化定制的便捷网页浏览器
  • Deepin(Linux)安装MySQL指南
  • 计算机三级网络技术知识汇总【6】
  • 【数据结构】快指针和慢指针
  • Mybatis学习总结
  • @Configuration与 @Component的差异
  • 条款23:宁以non-member、non-friend替换member函数
  • [嵌入式] 嵌入式软件开发书籍推荐及电子版分享
  • 【干货教程】Windows电脑本地部署运行DeepSeek R1大模型(基于Ollama和Chatbox)
  • 习题解答 | 一维差分与等差数列差分
  • sentinel小记
  • 基于大数据爬虫的旅游分析可视化平台设计和实现
  • Https解决了Http的哪些问题
  • RAG从入门到精通系列1:基础RAG
  • Python爬虫系统搭建教程,从0开始搭建爬虫系统(附安装包)
  • 公司禁用网站怎么做/知名网站
  • 成都工商注册官方网/seo刷关键词排名优化
  • 广告艺术设计专业学什么/排名优化系统
  • 郑州网站建设seo优化/建设网站推广
  • 工作站做网站/网站优化怎么操作
  • 国外做免费的视频网站有哪些/互联网舆情