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
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With