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

How to a grid column download a file using the path of the file

3 Answers 602 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Duan
Top achievements
Rank 1
Duan asked on 03 Aug 2012, 02:19 PM
Hi erveryone,

I wonder if there is any way to create a column in my grid, which is a link or a button or anything that is done by clicking a download of a file (.jpg, .doc, .txt ...) that is located in a folder of my solution. I emphasize that I have saved the file path in the database and I can load it in the column, if necessary.

Thanks

3 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 03 Aug 2012, 02:57 PM
Hello,

<telerik:RadGrid ID="RadGrid3" runat="server" AutoGenerateColumns="false" OnNeedDataSource="RadGrid3_NeedDataSource"
           OnItemDataBound="RadGrid3_ItemDataBound" OnItemCommand="RadGrid3_ItemCommand">
           <MasterTableView DataKeyNames="ID" CommandItemDisplay="Top">
               <Columns>
                   <telerik:GridBoundColumn HeaderText="ID" DataField="ID" UniqueName="ID">
                   </telerik:GridBoundColumn>
                   <telerik:GridBoundColumn HeaderText="Name" DataField="Name" UniqueName="Name">
                   </telerik:GridBoundColumn>
                   <telerik:GridTemplateColumn>
                       <ItemTemplate>
                           <asp:LinkButton ID="LinkButton1" runat="server" CommandName="DownloadMyFile" Text='<%# Eval("path") %>' CommandArgument='<%# Eval("path") %>'></asp:LinkButton>
                       </ItemTemplate>
                   </telerik:GridTemplateColumn>
               </Columns>
           </MasterTableView>
       </telerik:RadGrid>

protected void RadGrid3_ItemCommand(object sender, GridCommandEventArgs e)
   {
       if (e.CommandName == "DownloadMyFile")
       {
           GridDataItem item = e.Item as GridDataItem;
           // ACCESS KEy
           string strID = item.GetDataKeyValue("ID").ToString();
           //ACCESS COLUMN
           string strName = item["Name"].Text;
 
           // Download File
           string path = Server.MapPath("~/images/" + e.CommandArgument);
           string attachment = "attachment; filename=" + e.CommandArgument;
           Response.Clear();
           Response.AddHeader("content-disposition", attachment);
           Response.ContentType = "image/jpeg";
           Response.WriteFile(path);
           Response.End();
       }
   }
 
 
protected void RadGrid3_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
   {
 
       dynamic data = new[] {
               new { ID = 1, Name ="Name1",path="1.jpg"},
               new { ID = 2, Name = "Name2",path="2.jpg"},
               new { ID = 3, Name = "Name3",path="3.jpg"}
           };
       RadGrid3.DataSource = data;
 
 
   }


// IF YOUR GRID AJAXIFIED THEN ADD ALSO ADD BELOW CODE.
<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
       <ClientEvents OnRequestStart="onRequestStart" />
       <AjaxSettings>
           <telerik:AjaxSetting AjaxControlID="RadGrid3">
               <UpdatedControls>
                   <telerik:AjaxUpdatedControl ControlID="RadGrid3" />
               </UpdatedControls>
           </telerik:AjaxSetting>
       </AjaxSettings>
       </telerik:RadAjaxManager>

function onRequestStart(sender, args) {
               if (args.get_eventTarget().indexOf("LinkButton1") >= 0) {
                   args.set_enableAjax(false);
               }
           }





Thanks,
Jayesh Goyani
0
Bill
Top achievements
Rank 1
answered on 22 Aug 2012, 03:19 AM
Hello Jayesh,

Is there anyway you could provide a solution to upload the files to the same path to the associated folder as well, that way we have the whole solution for upload and download.

Thanks,
Bill
0
Eyup
Telerik team
answered on 24 Aug 2012, 08:24 AM
Hello Bill,

Please check out the following demo to get a practical idea of downloading and uploading files using a RadGrid:
 Grid / Attachment Column

I hope this will prove helpful.

Kind regards,
Eyup
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
Grid
Asked by
Duan
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Bill
Top achievements
Rank 1
Eyup
Telerik team
Share this question
or