web 2.0

Trick for Firefox who doesn't read all the bytes sent with ASP.Net HttpHandler in C#.

We found an issue where Firefox wasn't getting the entire file bytes sent by our ASP Server (in our case, the last byte of the stream was not read so the file were corrupt) but this bug never happens in Internet Explorer.

Here is the problematic code :

   1: Response.Clear();
   2: Response.ContentType = file.Mime;
   3: Response.Cache.SetCacheability(HttpCacheability.Private);
   4: Response.Expires = -1;
   5: Response.Buffer = false;
   6: Response.AddHeader("Content-Disposition", String.Format("{0};FileName=\"{1}\"", "attachment", file.Name));
   7: Response.OutputStream.Write(file.Contents, 0, file.Contents.Length);
   8: Response.End(); 

 

We found 2 solutions :

 

First, just change the Response.Buffer = False to Response.Buffer = true, this will have for effect that the response will not be sent until all the scripts are processed or the call of Response.End(), Response.Flush() call.

   1: Response.Clear();
   2: Response.ContentType = file.Mime;
   3: Response.Cache.SetCacheability(HttpCacheability.Private);
   4: Response.Expires = -1;
   5: Response.Buffer = true;
   6: Response.AddHeader("Content-Disposition", String.Format("{0};FileName=\"{1}\"", "attachment", file.Name));
   7: Response.OutputStream.Write(file.Contents, 0, file.Contents.Length);
   8: Response.End();

Or do not set the Response.Buffer, and use Response.BinaryWrite() (http://msdn.microsoft.com/en-us/library/ms524318.aspx) instead of Response.OutputStream() :

   1: Response.Clear();
   2: Response.ContentType = file.Mime;
   3: Response.Cache.SetCacheability(HttpCacheability.Private);
   4: Response.Expires = -1;
   5: Response.AddHeader("Content-Disposition", String.Format("{0};FileName=\"{1}\"", "attachment", file.Name));
   6: Response.BinaryWrite(file.Contents);
   7: Response.End();


Views(1550)

kick it on DotNetKicks.com

Share/Save/Bookmark Subscribe

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

ASP.NET | C#

Add comment


 

biuquote
Loading



Technorati Profile