Recently we are encountering strange issue with file download http handler developed using C# 4.0.
The web application is developed using ASP.NET 4.0 and hosted on IIS 7.0 over ssl. It worked correctly. But recently due to some changes else where in config or website we are facing the issue listed below.
When we download the text file it emits junk data. The same file works fine if i use code behind on aspx page instead of handler. Both have same code. Some of the files works fine. for e.g. image file or pdf file works fine. But with text file the behavior is very inconsistent. Blank text file works fine. I tried comparing the two responses (handler vs codebehind
) and it seems that content-length returned is not same.
context.Response.Clear();
context.Response.ClearHeaders();
context.Response.ClearContent();
context.Response.ContentType = !String.IsNullOrEmpty(mime) ? mime : "application/octet-stream";
context.Response.AppendHeader("Content-Disposition", String.Format("attachment; filename={0}", fileName));
//context.Response.AppendHeader("Content-Length", buffer.Length.ToString());
context.Response.OutputStream.Write(buffer, 0, buffer.Length);
context.Response.End();
CODE BEHIND
HTTP/1.1 200 OK Server: ASP.NET Development Server/10.0.0.0 Date: Thu, 06 Oct 2011 02:52:26 GMT X-AspNet-Version: 4.0.30319 Content-Disposition: attachment; filename=my junk.txt Cache-Control: private Content-Type: text/plain Content-Length: 29 Connection: Close
This is for sample test only
HTTPHANDLER
HTTP/1.1 200 OK Server: ASP.NET Development Server/10.0.0.0 Date: Thu, 06 Oct 2011 02:54:04 GMT X-AspNet-Version: 4.0.30319 Content-Disposition: attachment; filename=my junk.txt Cache-Control: private Content-Type: text/plain Content-Length: 146 Connection: Close
��������
I�%&/m�{J�J��t��
$ؐ@������iG#)���eVe]f@�흼��{���{���;�N'���?\fdl��J�ɞ!���?~|?"�yѤ��N�l��͛6������ ������I���