问题
We have a servlet that will deliver PDF reports to a browser. We also have a IIS server running .net apps and we want to return the PDF from the servlet as a stream to the .Net app and then the .Net app would render the PDF to the browser (we are using this technique for reason I don't need to go into here). I am not much of a VB/ Visual Studio devloper by this code works by using a web request:
Dim BUFFER_SIZE As Integer = 1024
' Create a request for the URL.
Dim serveraction As String = "https://OurSeverName/ServletContext/Dispatch?action=ajaxRunReport&reportName="
Dim request As WebRequest = _
WebRequest.Create(serveraction + ReportName.Text)
' Get the response.
Dim res As WebResponse = request.GetResponse()
' Get the stream containing content returned by the server.
Dim dataStream As Stream = res.GetResponseStream()
' Open the stream using a BinaryReader for easy access.
Dim reader As New BinaryReader(dataStream)
' Read the content.
Response.ContentType = "application/pdf"
Response.AddHeader("content-disposition", "inline; filename=reportfile.pdf")
Dim bytes = New Byte(BUFFER_SIZE - 1) {}
While reader.Read(bytes, 0, BUFFER_SIZE) > 0
Response.BinaryWrite(bytes)
End While
reader.Close()
' Clean up the streams and the response.
Response.Flush()
Response.Close()
The only issue is, even though the code runs quickly, it takes 20-30 seconds to render the PDF in Chrome and IE but only a few seconds in FireFox. Any idea why there is a delay in rendering the PDF? Is there a better way to stream a PDF from one server to another?
回答1:
There were just a couple of very subtle tweaks that were needed (and they seem pretty insignificant and non-intuitive to me).
I added the following before setting the content type:
Response.Clear()
Response.ClearHeaders()
And I added the following after reader.Close()
Response.End()
That was it. Now the PDF files stream nicely from the Java servlet to the IIS server and to the end user's browser.
来源:https://stackoverflow.com/questions/18232371/stream-pdf-from-a-server-to-a-web-client-vb-net