简体   繁体   English

使用memorystream使用VB.NET和Webforms进行pdf下载

[英]Using memorystream to make a pdf download using VB.NET and Webforms

I have a method that I wrote that calls a ReportServer and returns a byte array of a pdf of the report. 我有一个我编写的方法,它调用ReportServer并返回报告的pdf的字节数组。 This works. 这有效。 I know because I've used the same method to write a pdf to a file and save it locally. 我知道因为我使用相同的方法将pdf写入文件并在本地保存。 Here I want to write the file to a memory stream and offer it as a download. 在这里,我想将文件写入内存流并提供下载。

    Try
        Dim ms As MemoryStream = New MemoryStream(report, 0, report.Length)
        ms.Position = 0

        Response.Clear()
        Response.Expires = 0
        Response.Buffer = True
        Response.AddHeader("Content-disposition", "attachment;filename=UserReport.pdf")
        Response.ContentType = "application/pdf"
        'ms.WriteTo(Response.OutputStream)
        Response.BinaryWrite(ms.ToArray())
        Response.End()
        HttpContext.Current.ApplicationInstance.CompleteRequest()
        'Response.Flush()

    Catch ex As Exception
        lblERRORMSG.Visible = True
        lblERRORMSG.Text = "Darn!" & ex.Message
        Return
    End Try
End Sub`

This code compiles, runs, and I can even set breakpoints that get hit. 这段代码编译,运行,我甚至可以设置受到攻击的断点。 But when the button is clicked nothing happens. 但是当点击按钮时没有任何反应。

There is nothing wrong with the code in question. 这个代码没有任何问题。 This code was being used with an UpdatePanel made by telerik called a RadAjaxPanel. 此代码与telerik发布的名为RadAjaxPanel的UpdatePanel一起使用。 The default setting for buttons on pages that use this control is that they do NOT post back. 使用此控件的页面上按钮的默认设置是它们不回发。 The solution was simple. 解决方案很简单。 You need to go to the Page_Load Event in you code behind and add the following code: 您需要在后面的代码中转到Page_Load事件并添加以下代码:

ScriptManager1.RegisterPostBackControl(btnNameGoesHere)

This will trigger a postback when the button is clicked! 这将在单击按钮时触发回发!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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