使用ASP.NET MVC3下载大文件大小(4.7GB)时使用的类

Sna*_*yes 3 c# asp.net-mvc-3

我有一个专用于下载文件的控制器,看起来像:

  public ActionResult Download( string ids )
  {

     MyFileModel myfile = new MyFileModel(ids);
     Stream s = myfile.GetStream();

     return File( s, contentType, newFileName );
  }
Run Code Online (Sandbox Code Playgroud)

我看到File从返回是FileStreamResult但我的内存已满(我有8GB并下载7GB的raeches)和CPU 100%

如何优化下载?

Dar*_*rov 6

如何优化下载?

不要将整个文件加载到内存中.将其保留在磁盘上,并将服务器上此文件的位置指定为File方法的第一个参数:

// get the physical location of the file on the disk:
string file = Server.MapPath("~/App_Data/somefile.dat");
return File(file, contentType, newFileName );
Run Code Online (Sandbox Code Playgroud)

现在,如果您告诉我您在数据库中存储了5GB文件,那么您希望我告诉您什么?我想你已经知道了答案.

但正如@Marc在评论部分所述,即使您在数据库中存储了如此庞大的文件,您仍然可以有效地实现这一点.我们的想法是编写一个自定义的ActionResult,它将从数据库流中读取块中的文件,并将这些块直接刷新到Response OutputStream.