简体   繁体   中英

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. This works. I know because I've used the same method to write a pdf to a file and save it locally. 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. 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:

ScriptManager1.RegisterPostBackControl(btnNameGoesHere)

This will trigger a postback when the button is clicked!

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