Thursday, June 02, 2005

ASP.NET Response.TransmitFile() sometimes fails with networked UNC files



Simply replace this:
Response.TransmitFile(filename);


With this:

Stream s = new FileStream( filename, FileMode.Open, FileAccess.Read );
const int bufLength = 1024;
byte[] buf = new byte[bufLength];
int red;
while( (red=s.Read(buf,0,bufLength))>0 )
if (Response.IsClientConnected)
Response.BinaryWrite(buf);
Response.Flush();
Response.Close();
s.Close();


UNC networked files error failure fail C# TransmitFile WriteFile

Comments:
The problem with this solution is it is very slow to send the file. I tested it with a 6 mb file and it tooks 30 seconds consistently. A better solution would be copy the file from the unc to a local drive and TransmitFile() it. It takes only 5 seconds as it should be.
 
Did you try changing the buffer size? Perhaps 1k is too small, I'd try 4k, 8k, and even 64k to see if that makes a difference
 
Post a Comment

<< Home

This page is powered by Blogger. Isn't yours?