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

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

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


http://www.kler.cn/a/558988.html

相关文章:

  • Flutter最简单的路由管理方式Navigator
  • Windows11安装GPU版本Pytorch2.6教程
  • C进阶 自定义类型
  • mac 下 java 调用 gurobi 不能加载 jar
  • verilog测试平台设计与verilog的synthesis
  • 使用 CloudDM 和飞书流程化管理数据库变更审批
  • Swift Data 切片:data.subdata(in:) vs data[Range<Index>] 深度解析
  • 03 二层广播和三层广播
  • 进程概念、PCB及进程查看
  • MusicGPT的本地化部署与远程调用:让你的Windows电脑成为AI音乐工作站
  • 高性能GPU计算:释放计算潜力的加速利器
  • 基于拼接的宏基因组全流程
  • Day1 初识AndroidAudio
  • OpenSSL实验
  • 网络安全研究
  • Python常用的函数和功能
  • 黑马点评 面试话术
  • 蓝桥杯 Java B 组之背包问题(01背包、完全背包)
  • Pytorch使用手册-音频特征提取(专题二十一)
  • [Android] GKD v1.10.0 β1—— 开屏 及 内部信息流 广告跳过工具