简体   繁体   中英

ASP.NET 4.0, Browser binary render of PDF on IIS6

I have an ASP.Net 4.0 web application that renders on client's browser a PDF created on the fly using CrystalReports inside an iFrame via binary write. The webapp works well on my dev station (Visual Studio 2012 Web Developer), but when I tested to our production server (Win2003, IIS6) the part where the user clicks on a button to stream the PDF it just shows a part of the document (the page header logo only, the rest of the PDF is blank). I made a test: instead of streaming the binary data to the client's browser I saved to a local directory on the server's virtual path, I could access the PDF which integrity was OK, all data appeared on the saved doc. I've been Googling for 3 days, tried all kind of code snippets, IIS configurations (log files doesn't show any error), server permissions, HTML headers, web.config tags, etc., with no luck. I suspect that it has something to be with IIS, but I can't find the solution. Here is the code that renders the PDF on client's browser:

//reportObject is a CrystalReport document object (.rpt)
Dim s As System.IO.MemoryStream = reportObj.ExportToStream(ExportFormatType.PortableDocFormat)

s.Seek(0, SeekOrigin.Begin)

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

I also tried these, all worked on my dev machine, but not on the server:

With HttpContext.Current.Response
            .Clear()
            .ClearHeaders()
            .Buffer = True

            .AddHeader("Content-Disposition", "inline; filename=doc.pdf")
            .AddHeader("Content-Length", Int32.Parse(s.Length))
            .AddHeader("Content-transfer-encoding", "8bit")
            .AddHeader("Cache-Control", "private")
            .AddHeader("Pragma", "cache")
            .AddHeader("Expires", "0")
            .AddHeader("X-UA-Compatible", "IE=EmulateIE7")

            .ContentType = "application/pdf"
            .BinaryWrite(s.ToArray)

            .Flush()
            .Close()
            HttpContext.Current.ApplicationInstance.CompleteRequest()
        End With

And also these lines:

.AppendHeader("Accept-Ranges", "none")
.OutputStream.Write(s.ToArray(), 0, Convert.ToInt32(s.Length))

Nothing seems to help.

I had to implement another approach described on this thread:

Response.TransmitFile and delete it after transmission

I hope it helps someone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM