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

GridAttachmentColumn sounds like a good option so I will explore that first.
Thank you very much :)

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.
Thank you for sharing your solution with our community.
I hope it proves helpful to other developers as well.
Regards,
Eyup
Telerik