asp.net can't continue after response.binarywrite

五迷三道 提交于 2019-12-23 06:06:42

问题


I have the following code which successfully writes a pdf to the client. The problem is that I can't get any code after this to execute. It's the last step of a wizard, and despite putting this in the ActiveStepChanged handler, it never makes it to the confirmation/final page.

                    Response.Clear()
                Response.ContentType = Nothing
                Response.AddHeader("content-disposition", "attachment; filename=" & FileName)
                Response.BinaryWrite(data)
                Response.Flush()

Basically, there's a checkbox that the user checks if they want to download the file when they hit the Finish button. I don't want to have a separate button to download the file because users have been known to get confused and think that by pressing the download button that they've completed the necessary steps and never complete their application (we're talking about non computer literate users here). So it all works, except it doesn't make it to the confirmation step when they select that option.

How can I ensure that processing continues after downloading the file?


回答1:


The best way is to write this code in a HttpHandler and call Response.End after Response.Flush.




回答2:


I figured this one out myself. Basically, I placed the response.binarywrite code (listed in op) in the code behind of its own empty webform. I then call then use the javascript openwindow function in ScriptManager.RegisterClientScriptBlock to open it.

Code in original page:

            Me.Session("PrintApplication") = data
            Me.Session("PrintApplicationFileName") = FileName

            ScriptManager.RegisterClientScriptBlock(Me, Me.GetType, "PrintReport", "window.open('PrintApplication.aspx');", True)

Code in PrintApplication.aspx:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    If Me.Session("PrintApplicationFileName") <> Nothing Then
        Dim data As Byte() = Me.Session("PrintApplication")
        Dim FileName As String = Me.Session("PrintApplicationFileName")

        Me.Session("PrintApplication") = Nothing
        Me.Session("PrintApplicationFileName") = Nothing
        Response.Clear()
        Response.ContentType = Nothing
        Response.AddHeader("content-disposition", "attachment; filename=" & FileName)
        Response.BinaryWrite(data)
        Response.Flush()
        Response.End()

    End If

End Sub

Works perfectly



来源:https://stackoverflow.com/questions/24796224/asp-net-cant-continue-after-response-binarywrite

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!