【问题标题】:download file inside updatepanel with out PostBackTrigger在没有 PostBackTrigger 的更新面板中下载文件
【发布时间】:2017-11-29 10:31:16
【问题描述】:

我正在使用这部分代码通过 UpdatePanel 内的 imageButton 的 onClick 事件下载文件,如果我将 imageButton 添加为 updatePanel 的 PostBackTrigger,它工作正常,有没有办法做到这一点没有完整的回发?

        Dim stream As IO.Stream = Nothing
        Dim bytesToRead As Integer = 10000
        Dim buffer As Byte() = New Byte(bytesToRead - 1) {}
        Try
            Dim fileReq As Net.HttpWebRequest = CType(Net.HttpWebRequest.Create(FileURL), Net.HttpWebRequest)
            Dim fileResp As Net.HttpWebResponse = CType(fileReq.GetResponse(), Net.HttpWebResponse)
            If fileReq.ContentLength > 0 Then fileResp.ContentLength = fileReq.ContentLength
            stream = fileResp.GetResponseStream()
            Dim resp = HttpContext.Current.Response
            resp.ContentType = "application/octet-stream"
            resp.AddHeader("Content-Disposition", "attachment; filename=""" & FileName & """")
            resp.AddHeader("Content-Length", fileResp.ContentLength.ToString())
            Dim length As Integer
            Do
                If resp.IsClientConnected Then
                    length = stream.Read(buffer, 0, bytesToRead)
                    resp.OutputStream.Write(buffer, 0, length)
                    resp.Flush()
                    buffer = New Byte(bytesToRead - 1) {}
                Else
                    length = -1
                End If
            Loop While length > 0
        Finally
            If stream IsNot Nothing Then
                stream.Close()
            End If
        End Try

【问题讨论】:

  • 不,没有。您唯一能做的就是为文件下载创建一个通用处理程序并链接到该处理程序以进行下载。这适用于更新面板。
  • @VDWWD ,现在我正在使用它,但在下载文件 Response.Redirect("~/report/download.ashx?url=" & url & "&fName=myFile .pdf")
  • 不要使用像 response.redirect 这样的服务器端代码来下载文件。只需创建一个链接:<a target="_blank" href="/report/download.ashx?url=myfile">download</a>

标签: asp.net ajax.net


【解决方案1】:

我找到了解决方案。 @HATCHA 对Asp:progress won't end anitmation when file is downloaded from ashx file 的回答很棒。 @VDWWD 说的很棒,但对我来说不是,因为我需要一个按钮来创建 pdf 文件在开始下载之前 >。这意味着客户端点击将无用。

ScriptManager.RegisterClientScriptBlock(Me, GetType(Page), "downloadFileExport", "javascript:window.location ='" & ResolveUrl("~/report/download.ashx?url=" & url & "&fName=" & fName) & "';", True)

【讨论】:

    猜你喜欢
    • 2017-06-08
    • 1970-01-01
    • 1970-01-01
    • 2015-07-22
    • 2016-10-19
    • 1970-01-01
    • 2011-04-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多