10

My action returns a file from disk to client browser and currently I have:

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

This way it loads whole file in memory and is very slow, as the download start after the file is loaded to memory. What's the best way to handle such file downloads?

Thank you

Burjua
  • 12,506
  • 27
  • 80
  • 111

1 Answers1

12

Ok, I came across this forum discussion: http://forums.asp.net/t/1408527.aspx

Works like a charm, exactly what I need!

UPDATE

Came across this question How to deliver big files in ASP.NET Response? and it turns out it's much simpler, here is how I do it now:

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);
Community
  • 1
  • 1
Burjua
  • 12,506
  • 27
  • 80
  • 111