This is a migrated thread and some comments may be shown as answers.

RadGrid - File Download - Looking for working example please

4 Answers 466 Views
Grid
This is a migrated thread and some comments may be shown as answers.
John
Top achievements
Rank 1
John asked on 28 May 2015, 11:14 AM

Hi,

I am just wondering if there is full working example of a RadGrid with file download functionality anywhere?

My requirements are pretty simple - 

1. One of the grid columns will contain the actual name of the file (including extension) which I want to present as a clickable link (using asp:LinkButton maybe?).

2. Once the link is clicked the actual path to the physical file will have to be assembled in the code behind (don't want to expose file system details to the client apart from the file name).

3. The browser will allow the user to download the file in the normal way.

That's it. Just wondering if there is a good working example of this anywhere? Please let me know if any clarification is required. 

Thanks :)

4 Answers, 1 is accepted

Sort by
0
Eyup
Telerik team
answered on 02 Jun 2015, 07:47 AM
Hello John,

You can use GridAttachmentColumn to achieve this requirement:
http://demos.telerik.com/aspnet-ajax/grid/examples/columns-rows/columns/column-types/defaultcs.aspx

Alternatively, you may implement your own configuration using a GridTemplateColumn. I am attaching a sample RadGrid web site demonstrating how you can create various link columns.

Hope this helps.

Regards,
Eyup
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
John
Top achievements
Rank 1
answered on 02 Jun 2015, 10:13 AM

GridAttachmentColumn sounds like a good option so I will explore that first. 

Thank you very much :)

0
John
Top achievements
Rank 1
answered on 04 Jun 2015, 01:59 PM

Hi,

In case somebody finds this thread and wants to attempt something similar I thought that I would share my solution which wasn't based on the GridAttachmentColumn.

 

I defined my column as follows.

 

<telerik:GridTemplateColumn DataField="Name">
<ItemTemplate>
<asp:LinkButton ID="LinkButton" runat="server" Text='<%# Eval("Name")%>' CommandName="LinkButtonClicked"/>
</ItemTemplate>
</telerik:GridTemplateColumn>

 

If your grid is in an update panel then you will need to do this.

protected void OnItemCreated(object sender, GridItemEventArgs e)
{
    if (e.Item is GridDataItem)
    {
        var button = (e.Item as GridDataItem).FindControl("LinkButton") as LinkButton;
 
        if (button != null)
        {
            var scriptManager = ScriptManager.GetCurrent(this);
 
            if (scriptManager != null)
            {
                scriptManager.RegisterPostBackControl(button);
            }
        }
    }
}

 

And then finally, for the download

 

protected void OnItemCommand(object source, GridCommandEventArgs gridCommandEventArgs)
{
    if (gridCommandEventArgs.CommandName == "LinkButtonClicked")
    {
        var linkButton = gridCommandEventArgs.CommandSource as LinkButton;
 
        if (linkButton != null)
        {
            string fileName = linkButton.Text;
            string fullPath = <physical directory> + fileName;
 
            if (File.Exists(fullPath))
            {
                var binaryData = File.ReadAllBytes(fullPath);
                Response.Clear();
                Response.ContentType = "application/octet-stream";
                Response.AddHeader("content-disposition", "attachment; filename=" + fileName);
                Response.BinaryWrite(binaryData);
                Response.Flush();
                Response.End();
            }
        }
    }
}

 

I hope that is of some use to somebody. It worked for me anyhow.

0
Eyup
Telerik team
answered on 09 Jun 2015, 09:17 AM
Hi John,

Thank you for sharing your solution with our community.
I hope it proves helpful to other developers as well.

Regards,
Eyup
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
Grid
Asked by
John
Top achievements
Rank 1
Answers by
Eyup
Telerik team
John
Top achievements
Rank 1
Share this question
or