download file inside updatepanel with out PostBackTrigger

954 Views Asked by At

I am using this part of code to download file using the onClick event for imageButton inside UpdatePanel, it works fine if I add the imageButton as PostBackTrigger for the updatePanel, is there away to do that with out full postback?

        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
1

There are 1 best solutions below

0
On

I found the solution. @HATCHA's answer on Asp:progress won't end anitmation when file is downloaded from ashx file was great. what @VDWWD said was wonderful , But not in my case, because I need a button to create the pdf file before starting the download. which means the client side click won't be useful.

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