Skip to main content
All docs
V24.1

Download a File on the Client

  • 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.

<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>
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();
}

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.

<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>
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);
}

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