views:

518

answers:

1

I have the following code delivering a file to users when they click on a download link. For security purposes I can't just link directly to the file so this was set up to decode the url and transmit the file. It has been working fine for a while but recently I started having problems where the file will start downloading but there's no indication of how large the file is. Because of this when the download should stop, it doesn't. The file is about 99mb but when I download it, the browser just keeps downloading way beyond 100mb. I don't know what it's downloading but if I don't cancel it, it doesn't stop. So, my question is, is there either an alternative to transmitfile or a way to make sure the size of the file is sent also so that it stops at the right time? I don't want to use writefile because I don't want to load the entire file into memory since it's so large. Thanks.

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(); 
A: 

I think I've found the answer. I added the following code and now, at least for me, it is reporting the correct file size and working as expected.

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

geoff