Back to Devexpress

Download a File on the Client

aspnet-402322-common-concepts-client-side-functionality-download-file-on-the-client.md

latest5.0 KB
Original Source

Download a File on the Client

  • Oct 06, 2023
  • 2 minutes to read

Use the window.location.href property to redirect a web browser to a page that obtains a file (for instance, by a key value) and sends this file to the browser.

aspx
<dx:ASPxGridView ID="ASPxGridView1" runat="server" AutoGenerateColumns="False" DataSourceID="AccessDataSource1"
    ClientInstanceName="grid" KeyFieldName="EmployeeID">
    <ClientSideEvents CustomButtonClick="function(s, e) {
        window.location.href = 'Default.aspx?ID=' + grid.GetRowKey(e.visibleIndex);
    }"/>
    <Columns>
        <dx:GridViewCommandColumn VisibleIndex="0">
            <CustomButtons>
                <dx:GridViewCommandColumnCustomButton ID="clientRedirect" Text="Load photo" >
                </dx:GridViewCommandColumnCustomButton>
            </CustomButtons>
        </dx:GridViewCommandColumn>
        <!-- other columns -->
    </Columns>
</dx:ASPxGridView>
csharp
protected void Page_Load(object sender, EventArgs e) {
    if (Request["ID"] != null)
        SendFile(Request["ID"]);
}
private void SendFile(string param) {
    string fileName = "Photos/" + param + ".png";
    string filePath = Server.MapPath(fileName);
    Response.AddHeader("Content-Disposition", "attachment; filename=" + Request["ID"] + ".png");
    Response.ContentType = "image/x-png";
    Response.TransmitFile(filePath);
    Response.End();
}
vb
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    If Request("ID") IsNot Nothing Then
        SendFile(Request("ID"))
    End If
End Sub
Private Sub SendFile(ByVal param As String)
    Dim fileName As String = "Photos/" & param & ".png"
    Dim filePath As String = Server.MapPath(fileName)
    Response.AddHeader("Content-Disposition", "attachment; filename=" & Request("ID") & ".png")
    Response.ContentType = "image/x-png"
    Response.TransmitFile(filePath)
    Response.End()
End Sub

View Example: How to download files from an ASPxGridView column

You can prepare a URL with a query parameter on the server side. Use the ASPxCallback control to send a callback to the server and assign the returned URL to the window.location.href property.

aspx
<dx:ASPxCallback ID="ASPxCallback1" runat="server" ClientInstanceName="callback" OnCallback="ASPxCallback1_Callback">
    <ClientSideEvents CallbackComplete="function(s, e) {
        window.location.href = e.result;
    }" />
</dx:ASPxCallback>
<dx:ASPxGridView ID="ASPxGridView1" runat="server" AutoGenerateColumns="False" DataSourceID="AccessDataSource1"
    ClientInstanceName="grid" KeyFieldName="EmployeeID">
    <ClientSideEvents CustomButtonClick="function(s, e) {
        callback.PerformCallback(gridView.GetRowKey(e.visibleIndex));
    }"/>
    <Columns>
        <dx:GridViewCommandColumn VisibleIndex="0">
            <CustomButtons>
                <dx:GridViewCommandColumnCustomButton ID="clientRedirect" Text="Load photo" >
                </dx:GridViewCommandColumnCustomButton>
            </CustomButtons>
        </dx:GridViewCommandColumn>
        <!-- other columns -->
    </Columns>
</dx:ASPxGridView>
csharp
protected void Page_Load(object sender, EventArgs e) {
    if (Request["ID"] != null)
        SendFile(Request["ID"]);
}
private void SendFile(string param) {
    string fileName = "Photos/" + param + ".png";
    string filePath = Server.MapPath(fileName);
    Response.AddHeader("Content-Disposition", "attachment; filename=" + Request["ID"] + ".png");
    Response.ContentType = "image/x-png";
    Response.TransmitFile(filePath);
    Response.End();
}
protected void ASPxCallback1_Callback(object source, CallbackEventArgs e) {
    e.Result = string.Format("Default.aspx?ID={0}", e.Parameter);
}
vb
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    If Request("ID") IsNot Nothing Then
        SendFile(Request("ID"))
    End If
End Sub
Private Sub SendFile(ByVal param As String)
    Dim fileName As String = "Photos/" & param & ".png"
    Dim filePath As String = Server.MapPath(fileName)
    Response.AddHeader("Content-Disposition", "attachment; filename=" & Request("ID") & ".png")
    Response.ContentType = "image/x-png"
    Response.TransmitFile(filePath)
    Response.End()
End Sub
Protected Sub ASPxCallback1_Callback(ByVal source As Object, ByVal e As CallbackEventArgs)
    e.Result = String.Format("Default.aspx?ID={0}", e.Parameter)
End Sub

View Example: How to load a file on the callback of the ASPxGridView