locked
How to download a file from a content page inside UpdatePanel in ASP RRS feed

  • Question

  • User-883001733 posted

    I want to download a file from an ASP webforms page.

    If I use a WebForm without a MasterPage everything is fine, I have a LinkButton with this:

        Private Sub linkDOWNLOAD_Click(sender As Object, e As EventArgs) Handles linkDOWNLOAD.Click
            Dim filePath = "C:\files\" + DropDownList1.SelectedItem.Value
            Dim fileStream As Stream = streamClient.someWebServiceReturningFileStream(filePath)
            Dim buffer(65536) As Byte
            Dim bytesRead = 0
    
            Response.BufferOutput = False
            HttpContext.Current.Response.Clear()
            HttpContext.Current.Response.ClearHeaders()
            HttpContext.Current.Response.ContentType = "application/octet-stream"
            HttpContext.Current.Response.AddHeader("Content-Disposition",
                        "attachment; filename=" + DropDownList1.SelectedItem.Value)
    
            bytesRead = fileStream.Read(buffer, 0, buffer.Length)
    
            While (bytesRead > 0)
                If (Response.IsClientConnected) Then
                    Response.OutputStream.Write(buffer, 0, bytesRead)
                    Response.Flush()
                    buffer = New Byte(65536) {}
                    bytesRead = fileStream.Read(buffer, 0, buffer.Length)
                Else
                    bytesRead = -1
                End If
            End While
    
            Response.Flush()
            Response.Close()
            Response.End()
          End

    As I said this works fine and the browser open the dialog to download the file, but if I try the same code in a button inside a content page within a MasterPage, then nothing happens.

    The Master page has this inside:

    ...
    <asp:UpdatePanel ID="updatePanelBODY" runat="server">
       <ContentTemplate>
            <asp:ContentPlaceHolder ID="contentBODY" runat="server">
            </asp:ContentPlaceHolder>                    
       </ContentTemplate>
    </asp:UpdatePanel>
    ...

    The Content page has this inside:

    <asp:Content ID="Content2" ContentPlaceHolderID="contentBODY" runat="server">
        <asp:LinkButton ID="linkDOWNLOAD" runat="server">Download File</asp:LinkButton>
    </asp:Content>

    I don't know how to solve this and every example I have found is based on webforms without any master page.

    Thanks for your help.

    Wednesday, April 8, 2020 8:23 PM

All replies

  • User-1330468790 posted

    Hi Caserta83,

     

    Caserta83

    As I said this works fine and the browser open the dialog to download the file, but if I try the same code in a button inside a content page within a MasterPage, then nothing happens.

    It would be a problem that the file is  the result of the partial page update since you used an UpdatePanel control. However, the UpdatePanel control is supposed to update contents and not a file download.

     

    Caserta83

    I don't know how to solve this and every example I have found is based on webforms without any master page.

    The solution is to set the button to trigger a full page postback.

    In your case, what you need to do is add below code in the "Page_Load" event handler.

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        ScriptManager.GetCurrent(Page).RegisterPostBackControl(linkDOWNLOAD)
    End Sub

    I reproduced your problem. After adding above code, you will find that a full page postback trigger (Press F12 in browser) and the file downloading works fine .

      

    Hope this can help you.

    Best regards,

    Sean

    Friday, April 10, 2020 1:52 AM