Response TransmitFile

I have the following code delivering a file to users when they click on the download link. For security reasons, I cannot just link directly to the file so that it is configured to decode the URL and transfer the file.

It has been working fine for a while, but recently I have had problems downloading the file, but there is no indication of how large the file is.

Because of this, when loading should stop, it is not.

The file is about 99 MB, but when I download it, the browser just downloads a path above 100 MB. I do not know what it is loading, but if I do not cancel it, it does not stop.

So my question is: is there an alternative TransmitFileor a way to send the file size also to stop at the right time?

Here is the code:

string filename = Path.GetFileName(url); 
context.Response.Buffer = true; 
context.Response.Charset = ""; 
context.Response.Cache.SetCacheability(HttpCacheability.NoCache); 
context.Response.ContentType = "application/x-rar-compressed"; 
context.Response.AddHeader("content-disposition", "attachment;filename=" + filename);   
context.Response.TransmitFile(context.Server.MapPath(url));
context.Response.Flush(); 

WriteFile, , .

.

+3
1

, .

, , , .

FileInfo OutFile = new FileInfo(context.Server.MapPath(url));
long filesize = OutFile.Length; 
....
context.Response.AddHeader("Content-Length", filesize.ToString());
+2

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


All Articles