【问题标题】:How to stream file from disk to client browser in .Net MVC如何在.Net MVC中将文件从磁盘流式传输到客户端浏览器
【发布时间】:2014-10-14 13:44:39
【问题描述】:

我的操作将文件从磁盘返回到客户端浏览器,目前我有:

public FileResult MediaDownload ()
{
  byte[] fileBytes = System.IO.File.ReadAllBytes(Server.MapPath(filePath));
  return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);
}

这种方式将整个文件加载到内存中并且非常慢,因为在文件加载到内存后开始下载。处理此类文件下载的最佳方式是什么?

谢谢

【问题讨论】:

    标签: c# .net asp.net-mvc io stream


    【解决方案1】:

    好的,我遇到了这个论坛讨论:http://forums.asp.net/t/1408527.aspx

    就像一个魅力,正是我需要的!

    更新

    遇到了这个问题How to deliver big files in ASP.NET Response?,结果发现它要简单得多,这就是我现在的做法:

    var length = new System.IO.FileInfo(Server.MapPath(filePath)).Length;
    Response.BufferOutput = false;
    Response.AddHeader("Content-Length", length.ToString());
    return File(Server.MapPath(filePath), System.Net.Mime.MediaTypeNames.Application.Octet, fileName);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-26
      • 2020-08-30
      • 1970-01-01
      • 2015-06-08
      • 2015-11-29
      • 1970-01-01
      • 2019-05-07
      • 1970-01-01
      相关资源
      最近更新 更多