将 PDF 从服务器流式传输到 Web 客户端 vb.net [英] Stream PDF from a server to a web client vb.net

查看:41
本文介绍了将 PDF 从服务器流式传输到 Web 客户端 vb.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个 servlet,可以将 PDF 报告传送到浏览器.我们还有一个运行 .net 应用程序的 IIS 服务器,我们希望将 PDF 作为流从 servlet 返回到 .Net 应用程序,然后 .Net 应用程序将把 PDF 呈现给浏览器(我们使用这种技术是因为我不需要进入这里).我不是一个 VB/Visual Studio 开发者,这个代码使用 web 请求工作:

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() 

唯一的问题是,即使代码运行得很快,在 Chrome 和 IE 中渲染 PDF 也需要 20-30 秒,而在 FireFox 中只需几秒钟.知道为什么渲染 PDF 会出现延迟吗?有没有更好的方法将 PDF 从一台服务器流式传输到另一台服务器?

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?

推荐答案

只需要进行一些非常微妙的调整(对我来说它们似乎微不足道且不直观).

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.Clear()

Response.ClearHeaders()

Response.ClearHeaders()

我在 reader.Close() 之后添加了以下内容

And I added the following after reader.Close()

Response.End()

Response.End()

就是这样.现在,PDF 文件可以很好地从 Java servlet 流到 IIS 服务器和最终用户的浏览器.

That was it. Now the PDF files stream nicely from the Java servlet to the IIS server and to the end user's browser.

这篇关于将 PDF 从服务器流式传输到 Web 客户端 vb.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆