【问题标题】:asp.net can't continue after response.binarywrite在 response.binarywrite 之后,asp.net 无法继续
【发布时间】:2014-07-17 06:23:09
【问题描述】:

我有以下代码可以成功地将 pdf 写入客户端。问题是在此之后我无法获得任何代码来执行。这是向导的最后一步,尽管将其放在 ActiveStepChanged 处理程序中,但它永远不会进入确认/最终页面。

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

基本上,当用户点击“完成”按钮时,有一个复选框可供用户检查是否要下载文件。我不想有一个单独的按钮来下载文件,因为众所周知,用户会感到困惑,并认为通过按下下载按钮他们已经完成了必要的步骤并且从未完成他们的应用程序(我们谈论的是非有计算机知识的用户在这里)。所以一切正常,除了当他们选择该选项时它没有进入确认步骤。

如何确保下载文件后继续处理?

【问题讨论】:

    标签: asp.net httpresponse


    【解决方案1】:

    最好的办法是把这段代码写在一个 HttpHandler 中,然后在 Response.Flush 之后调用 Response.End。

    【讨论】:

    • 谢谢,但不幸的是,这并没有什么不同。之后仍然无法执行任何代码
    • @user1815872 httpHandler 与空页面相同......并且您必须在 Response.Flush 之后编写 Response.End 才能结束响应。
    【解决方案2】:

    这是我自己想出来的。基本上,我将 response.binarywrite 代码(在 op 中列出)放在了它自己的空 webform 后面的代码中。然后我调用然后使用 ScriptManager.RegisterClientScriptBlock 中的 javascript openwindow 函数来打开它。

    原始页面中的代码:

                Me.Session("PrintApplication") = data
                Me.Session("PrintApplicationFileName") = FileName
    
                ScriptManager.RegisterClientScriptBlock(Me, Me.GetType, "PrintReport", "window.open('PrintApplication.aspx');", True)
    

    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
    

    完美运行

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-29
      • 2012-07-17
      • 2010-12-03
      相关资源
      最近更新 更多