PDF saved as aspx/ashx over HTTPS

979 views Asked by At

I have an ashx handler that displays on browser a PDF from binary data, I'm using .NET Framework 4.0, IIS 6 and Chrome browser:

        Dim s As System.IO.MemoryStream = HttpContext.Current.Session("tmp_binaryData")
        s.Seek(0, System.IO.SeekOrigin.Begin)

        With HttpContext.Current.Response
            .ClearContent()
            .ClearHeaders()
            .ContentType = "application/pdf"
            .AddHeader("Content-Disposition", "inline; filename='test.pdf'")
            .BinaryWrite(s.ToArray)
            .Flush()
        End With

When accessed from HTTPS and the user tries to save the file (clicking on the save button from the PDF viewer's toolbar) the file's saved as ashx and not pdf. This doesn't occur if the web application is accessed from HTTP (port 80).

Is worth to mention that if I change the ContentType to "attachment/pdf" the file is automatically saved as PDF correctly.

I've used all sort of header combinations with no success, disabled Chrome's PDF Viewer, tried Adobe Reader, checked the file compresion option on IIS, verified MIME types, just to list some.

As an additional information, I'm using this same tecnique on another webapplication hosted on IIS7 on another server and the problem is not present, so it has something to be with the IIS 6 configuration.

UPDATE Jan/05/2017

I have loaded a test pdf from file via Response.Redirect("test.pdf") on production server and the file saves successfully on Chrome. So, I compared the headers of both display methods:

Response.Redirect() method:

headers="
Accept-Ranges: bytes
Content-Length: 696505
Content-Type: application/pdf
Date: Thu, 05 Jan 2017 18:05:09 GMT
ETag: "33dad7f8baccd11:11a1"
Last-Modified: Wed, 22 Jun 2016 19:19:18 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET"

ASHX handler (filestram) method:

headers="
Cache-Control: private
Content-Disposition: inline; filename='Prueba.pdf'
Content-Type: application/pdf
Date: Thu, 05 Jan 2017 18:09:58 GMT
Server: Microsoft-IIS/6.0
Transfer-Encoding: chunked
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET"

I noticed that the ASHX method is missing the "Accept-Ranges" and "Content-Lenght" headers, but it caught my attention that it has the "Transfer-Encoding: chunked" header. Do you think this could be causing the issue? I'll try to replicate the first method headers on the ASHX.

1

There are 1 answers

0
Máster On

Well, I had to generate the PDF file physically on the server and then send it to the client's browser with Response.Redirect():

Dim s As System.IO.MemoryStream = HttpContext.Current.Session("tmp_binaryData")            
Dim serverPath As String = "~/temp/"

System.IO.File.WriteAllBytes(HttpContext.Current.Server.MapPath(serverPath + HttpContext.Current.Session("tmp_fileName")), s.ToArray())
HttpContext.Current.Response.Redirect((serverPath + HttpContext.Current.Session("tmp_fileName")))

Those generated files need to be deleted when the session ends or expires, which can be done in Global.asax .

Another workarround would be using the original method (write bytes to browser) and instead of clicking on the save icon the user would click on the print button and then save the PDF file.