Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在.NETMVC中将文件从磁盘流式传输到客户端浏览器_C#_.net_Asp.net Mvc_Io_Stream - Fatal编程技术网

C# 如何在.NETMVC中将文件从磁盘流式传输到客户端浏览器

C# 如何在.NETMVC中将文件从磁盘流式传输到客户端浏览器,c#,.net,asp.net-mvc,io,stream,C#,.net,Asp.net Mvc,Io,Stream,我的操作将文件从磁盘返回到客户端浏览器,目前我有: public FileResult MediaDownload () { byte[] fileBytes = System.IO.File.ReadAllBytes(Server.MapPath(filePath)); return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, fileName); } 这样,它将整个文件加载到内存中,速度非常慢,

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

public FileResult MediaDownload ()
{
  byte[] fileBytes = System.IO.File.ReadAllBytes(Server.MapPath(filePath));
  return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);
}
这样,它将整个文件加载到内存中,速度非常慢,因为文件加载到内存后,下载就开始了。处理此类文件下载的最佳方式是什么


谢谢

好的,我遇到了这个论坛讨论:

就像一个符咒,正是我需要的

更新

我遇到了这个问题,结果变得简单多了,我现在是这样做的:

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