.NET Forum / ASP.NET / General / June 2007
Problem displaying binary (pdf) content in IIS7
|
|
Thread rating:  |
Brad - 18 Apr 2007 19:31 GMT I have a .net 2.0 web application project that creates a pdf file, saves the pdf to disk (crystal reports does this part), and then my code reads the pdf file and writes it to the httpresponse The web app works great on win2003 and xp and the end result is a pdf file is displayed in the browser. When I run the same code on Vista, the browser displays the message "Internet Explorer cannot display the web page". If I open the same pdf file directly from the browser it displays correctly. If I trace the code no exceptions are thrown. I am running this as an IIS application. If I switch to cassini it works correctly. What might cause this problem in IIS7?
Here is an example of the code (and the page source has no html content, and agin work find under IIS running on win2003 and xp)
Dim outputFile As String = Path.Combine(folderPath, Current.Session.SessionID & ".pdf") Dim contentType As String
contentType = "application/pdf"
With Current.Response If System.IO.File.Exists(outputFile) = True Then .ClearContent() .ClearHeaders() .ContentType = contentType Dim fs As FileStream = New FileStream(outputFile, FileMode.Open) Try Dim bufferSize As Integer = CType(fs.Length, Integer) Dim bt(bufferSize) As Byte fs.Read(bt, 0, bufferSize) .BinaryWrite(bt) Catch ex As Exception System.Web.HttpContext.Current.Trace.Warn(ex.Message) Throw ex Finally fs.Close() End Try '######################################## .Flush() .Close() Else System.Web.HttpContext.Current.Trace.Warn("Report file not found: " & outputFile) Throw New Exception("PostExportToClient: File not found " & outputFile) End If End With
Walter Wang [MSFT] - 19 Apr 2007 09:01 GMT Hi Brad,
My understanding of the issue so far is:
1) The web site is installed on IIS7 on Vista 2) When you're visiting a web page that will download a PDF file, you found it's not working correctly. (One thing unclear to me is: are you using IE7 on the same Vista with IIS7 or from other XP/2003 system? and what's the IE version involved here)
I just tried to test it on my side using following steps:
1) Configured and installed IIS7 on Vista 2) Created a simple web site and created a web page using your code 3) Downloaded a simple PDF file to test, for example: http://history-matters.com/archive/pdftest.pdf 4) Installed Adobe Reader 8.0 on Vista
It seems your code is working correctly by opening the PDF document in IE7 browser window on Vista.
I believe there must be something missing here or this is an environment specific issue.
Sincerely, Walter Wang (wawang@online.microsoft.com, remove 'online.') Microsoft Online Community Support
================================================== Get notification to my posts through email? Please refer to http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif ications. If you are using Outlook Express, please make sure you clear the check box "Tools/Options/Read: Get 300 headers at a time" to see your reply promptly.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues where an initial response from the community or a Microsoft Support Engineer within 1 business day is acceptable. Please note that each follow up response may take approximately 2 business days as the support professional working with you may need further investigation to reach the most efficient resolution. The offering is not appropriate for situations that require urgent, real-time or phone-based interactions or complex project analysis and dump analysis issues. Issues of this nature are best handled working with a dedicated Microsoft Support Engineer by contacting Microsoft Customer Support Services (CSS) at http://msdn.microsoft.com/subscriptions/support/default.aspx. ==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
Walter Wang [MSFT] - 23 Apr 2007 03:22 GMT Hi Brad,
Have you seem my last reply? Does my test works on your side? Please feel free to let me know if there's anything I can help. Thanks.
Regards, Walter Wang (wawang@online.microsoft.com, remove 'online.') Microsoft Online Community Support
================================================== When responding to posts, please "Reply to Group" via your newsreader so that others may learn and benefit from your issue. ==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
Brad - 30 Apr 2007 23:07 GMT It works for a site under localhost, but not for a different web other than localhost. However the rest of the same site (project) works correctly. For example aspx form pages (pages that have html content) display correctly.
Hi Brad,
Have you seem my last reply? Does my test works on your side? Please feel free to let me know if there's anything I can help. Thanks.
Regards, Walter Wang (wawang@online.microsoft.com, remove 'online.') Microsoft Online Community Support
================================================== When responding to posts, please "Reply to Group" via your newsreader so that others may learn and benefit from your issue. ==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
Brad - 01 May 2007 00:21 GMT It works for a site under localhost, but not for a different web other than localhost. However the rest of the same site (project) works correctly. For example aspx form pages (pages that have html content) display correctly.....And it is the same problem with an image such as jpg. Image display in casini and localhost, but not the other webs.
Hi Brad,
Have you seem my last reply? Does my test works on your side? Please feel free to let me know if there's anything I can help. Thanks.
Regards, Walter Wang (wawang@online.microsoft.com, remove 'online.') Microsoft Online Community Support
================================================== When responding to posts, please "Reply to Group" via your newsreader so that others may learn and benefit from your issue. ==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
Walter Wang [MSFT] - 02 May 2007 08:14 GMT Hi Brad,
Thanks for the update.
> And it is the same problem with an image such as jpg. Image display in casini and localhost, but not the other webs.
Do you mean that if you directly visit http://localhost/website/test.jpg also doesn't work? What's the HTTP status code returned? In my test, if you remove .jpg from MIME types (IIS manager, configure website's MIME Types), you will get HTTP 404.3 error code when you visit a .jpg file from the website.
However, in your PDF case, since we're directly telling the browser that the content/type is "application/pdf", the MIME Types settings should not apply here. By the way, what's the error message when you visit the page that's generating the PDF file?
Try to create a new Application from the Root directory, this will make sure it inherits from the settings from your root directory (since you mentioned the root application is working fine).
Regards, Walter Wang (wawang@online.microsoft.com, remove 'online.') Microsoft Online Community Support
================================================== When responding to posts, please "Reply to Group" via your newsreader so that others may learn and benefit from your issue. ==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
Brad - 02 May 2007 18:08 GMT Just the opposite. If I directly vist the pdf or image url it DOES work. It only fails when the content is served up via a asp.net page Please note: I have created multiple webs on the vista workstation. The problem occurs on a web other than localhost. If I use localhost all works correctly. If I use a web other that localhost, displaying the image content from an asp.net page fails i.e. http://myotherweb/website/showimage.aspx , however accessing the image directly does work i.e. http://myotherweb/website/test.jpg
There is no .Net error message with the error occrus (event if I step through the code no exception is raised). Internet Explorer just shows the generic message "Internet Explorer cannot display the webpage"
> Hi Brad, > [quoted text clipped - 28 lines] > > This posting is provided "AS IS" with no warranties, and confers no rights. Walter Wang [MSFT] - 03 May 2007 08:59 GMT Hi Brad,
Thanks for the update, so the showimage.aspx is writing out binary image data just like the PDF?
Please install Fiddler (http://www.fiddlertool.com/) and capture the request/response between the browser and the web server to compare the difference when the page is served using localhost and a sub website. I think the returned HTTP status code will be different.
Regards, Walter Wang (wawang@online.microsoft.com, remove 'online.') Microsoft Online Community Support
================================================== When responding to posts, please "Reply to Group" via your newsreader so that others may learn and benefit from your issue. ==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
Brad - 07 May 2007 16:31 GMT Fiddler does not seem capture localhost traffic (tested this on fact on several computers). It only seems to capture from webs other than localhost. But here are the header differences between viewing the pdf directly and viewing it from aspx page:
Viewing pdf directly (pdf does display) HTTP/1.1 200 OK Content-Type: application/pdf Last-Modified: Mon, 07 May 2007 15:07:22 GMT Accept-Ranges: bytes ETag: "c0bf96ab990c71:0" Server: Microsoft-IIS/7.0 X-Powered-By: ASP.NET Date: Mon, 07 May 2007 15:14:51 GMT Content-Length: 55193
Viewing pdf from aspx page that writes binary content using code that started this thread (pdf does not display) HTTP/1.1 200 OK Cache-Control: private Transfer-Encoding: chunked Content-Type: application/pdf Server: Microsoft-IIS/7.0 X-AspNet-Version: 2.0.50727 X-Powered-By: ASP.NET Date: Mon, 07 May 2007 14:57:16 GMT
> Hi Brad, > [quoted text clipped - 16 lines] > > This posting is provided "AS IS" with no warranties, and confers no rights. George Ter-Saakov - 07 May 2007 17:54 GMT I believe for binary files you must output "Content-Length" in a header. Cause otherwise browser does not know when transmission of the file stopped.
George.
> Fiddler does not seem capture localhost traffic (tested this on fact on > several computers). It only seems to capture [quoted text clipped - 45 lines] >> This posting is provided "AS IS" with no warranties, and confers no >> rights. Brad - 08 May 2007 00:28 GMT Interesting thought. But why then does it work when served up by IIS5 (XP) and IIS6 (server2003)? This exact same same code (and the aspx apps that use them) has been working for years on both XP and 2003.
>I believe for binary files you must output "Content-Length" in a header. > Cause otherwise browser does not know when transmission of the file stopped. [quoted text clipped - 47 lines] >>> >>> This posting is provided "AS IS" with no warranties, and confers no rights. George Ter-Saakov - 08 May 2007 14:57 GMT I am not a guru on HTTP protocols but with HTTP 1.0 the server will always close connection which meant that transmission of the file ended. With HTTP 1.1 connection might be kept open so browser does not need to reconnect if it needs to get the bunch of pictures from the same site (Usually that is the case with regular HTML). Browser sends a request Connection: keep-open (something like that) and server honors it if it can.
So server might not close it right away. If server does not response with Content-Length and do not close connection browser have no way to know that download has stopped.
With Fiddler it all becomes a moot point. Since Fiddler might close connection all the time ignoring that server wanted to keep connection open. So what I am saying you might not see clear picture with Fiddler between browser and server.
Anyway, try to add Content-Lenght and see if it worked.
George.
> Interesting thought. But why then does it work when served up by IIS5 > (XP) and IIS6 (server2003)? This exact same same code (and the aspx [quoted text clipped - 57 lines] >>>> This posting is provided "AS IS" with no warranties, and confers no >>>> rights. Walter Wang [MSFT] - 08 May 2007 10:55 GMT Hi Brad,
You can just replace 'localhost' with your computer name and Fiddler should be able to capture the traffic.
Here's the response header at my side (both writing the PDF using stream and visiting directly works on my side):
Viewing pdf directly:
HTTP/1.1 200 OK Content-Type: application/pdf Last-Modified: Thu, 19 Apr 2007 07:15:36 GMT Accept-Ranges: bytes ETag: "8071d9865282c71:0" Server: Microsoft-IIS/7.0 X-Powered-By: ASP.NET Date: Tue, 08 May 2007 09:31:38 GMT Content-Length: 48148
Viewing pdf from aspx page:
HTTP/1.1 200 OK Cache-Control: private Transfer-Encoding: chunked Content-Type: application/pdf Server: Microsoft-IIS/7.0 X-AspNet-Version: 2.0.50727 X-Powered-By: ASP.NET Date: Tue, 08 May 2007 09:30:20 GMT
As you can see the log is same as yours.
Do you have another Vista box to test? This can help to confirm if it's an environment specific issue. If another Vista box also has this issue, this might be related to your web application (web.config or something unknown yet), please try this with a simple web application. In this case, if you can trim down your web application to a smaller project (while still having the issue), please send me a copy so that I can test it on my side.
Brad, I understand this thread is rather long now and you may have some concerns since we still haven't find the root cause; if this is urgent for your business, I really recommend you to use our Customer Support and Service to better troubleshoot such issue (it's non reproducible on my side). Please see my signature for more information.
Sincerely, Walter Wang (wawang@online.microsoft.com, remove 'online.') Microsoft Online Community Support
================================================== Get notification to my posts through email? Please refer to http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif ications. If you are using Outlook Express, please make sure you clear the check box "Tools/Options/Read: Get 300 headers at a time" to see your reply promptly.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues where an initial response from the community or a Microsoft Support Engineer within 1 business day is acceptable. Please note that each follow up response may take approximately 2 business days as the support professional working with you may need further investigation to reach the most efficient resolution. The offering is not appropriate for situations that require urgent, real-time or phone-based interactions or complex project analysis and dump analysis issues. Issues of this nature are best handled working with a dedicated Microsoft Support Engineer by contacting Microsoft Customer Support Services (CSS) at http://msdn.microsoft.com/subscriptions/support/default.aspx. ==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
Brad - 08 Jun 2007 00:16 GMT Walter - I found a solution to this issue. As originally noted, my code I originally posted in this thread worked in XP and win23k but not in Vista. Some minor programming changes resolve the problem. See the code example below and its comments which note the code that was commented out and code added to make it work.
string outputFile = Path.Combine(ReportPath, Current.Session.SessionID + "." + format.ToString()); string contentType = "application/pdf";
// commented out next two lines // Response.ClearContent(); // Response.ClearHeaders();
// added next to lines Clear and Buffer = true Response.Clear(); Response.Buffer = true;
Response.ContentType = contentType;
FileStream fs = new FileStream(outputFile, FileMode.Open); try { int bufferSize = System.Convert.ToInt32(fs.Length); byte[] bt = new byte[bufferSize + 1]; fs.Read(bt, 0, bufferSize); Response.BinaryWrite(bt); } catch (Exception ex) { throw ex; } finally { fs.Close(); } // commented out next two lines //Response.Flush(); //Response.Close();
// added next line Response.End();
> Hi Brad, > [quoted text clipped - 67 lines] > > This posting is provided "AS IS" with no warranties, and confers no rights. Walter Wang [MSFT] - 08 Jun 2007 09:05 GMT Hi Brad,
Interesting, I also just came along this blog yesterday about this issue on IIS7:
#Speaking of which... : I've upgraded and now my application doesn't work anymore http://blogs.msdn.com/johan/archive/2007/01/23/i-ve-upgraded-and-now-my-appl ication-doesn-t-work-anymore.aspx
Above blog also mentioned that we need to replace "Response.Flush() and Response.Close()" with Response.End() on Vista.
The code previously worked on my side is probably because I'm using IIS6 compatibility mode.
Regards, Walter Wang (wawang@online.microsoft.com, remove 'online.') Microsoft Online Community Support
================================================== When responding to posts, please "Reply to Group" via your newsreader so that others may learn and benefit from your issue. ==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|