How to transfer a file from disk to a client browser in .Net MVC

My actions are returning the file from disk to the 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); } 

Thus, it loads the entire file into memory and works very slowly, since the download starts after the file is loaded into memory. What is the best way to handle such files?

thanks

+4
source share
1 answer

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

Works like a charm, exactly what I need!

UPDATE

Went through this question. How to deliver large files in ASP.NET Response? , and it turns out that this is much simpler, here's 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); 
+7
source

Source: https://habr.com/ru/post/1273571/


All Articles