Search code examples
asp.netdatagridviewwebformsradgrid

Download file from SQL data source using asp.net web forms Gridview


I'm using the Telerik RadGrid control and with it I'm using the GridAttachmentColumn as recommended on their documentation to download files but I seen to be missing something, perhaps some Code Behind? The documentation talks about disabling Ajax for file upload and exports but not download?

The SQL table contains the UNC path where these files are stored in a network share. The 'download' column points to the right data\location, the file path becomes a hyperlink but no action when I click on it.

I have also tried using a Hyperlink column which seems to partially work on IE but not on Chrome. Partially because some files are downloaded and some rendered on screen. I need to download them all with a solution that works on both browsers.

Here is the Grid code:

 <telerik:RadGrid ID="RadGrid1" runat="server" DataSourceID="SqlDataSource1" Skin="WebBlue" AllowPaging="True">
<GroupingSettings CollapseAllTooltip="Collapse all groups"></GroupingSettings>
        <MasterTableView AutoGenerateColumns="False" DataSourceID="SqlDataSource1" NoDetailRecordsText="No attachments to display.">
            <DetailTables>
                <telerik:GridTableView runat="server" DataKeyNames="SessionId" DataSourceID="SqlDataSource2" AllowPaging="False" PageSize="5" NoDetailRecordsText="No attachment to display.">
                    <Columns>
                        <telerik:GridAttachmentColumn DataSourceID="SqlDataSource2" HeaderText="Download" AttachmentDataField="DocName" AttachmentKeyFields="1" FileNameTextField="DocName" DataTextField="Path" UniqueName="Attachments" MaxFileSize="1048576">
                        </telerik:GridAttachmentColumn>
                    </Columns>
                </telerik:GridTableView>
            </DetailTables>
            <CommandItemSettings ShowAddNewRecordButton="False" ShowExportToExcelButton="True" ShowExportToPdfButton="True" ShowExportToWordButton="True" ShowRefreshButton="False" />
            <Columns>
                <telerik:GridBoundColumn DataField="SessionId" FilterControlAltText="Filter SessionId column" HeaderText="SessionId" SortExpression="SessionId" UniqueName="SessionId" Visible="False">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="Ticket Number" FilterControlAltText="Filter TicketNumber column" HeaderText="Ticket Number" SortExpression="TicketNumber" UniqueName="TicketNumber">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="Title" FilterControlAltText="Filter Title column" HeaderText="Title" SortExpression="Title" UniqueName="Title">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="Description" FilterControlAltText="Filter Description column" HeaderText="Description" SortExpression="Description" UniqueName="Description" Visible="False">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="AffectedUser" FilterControlAltText="Filter AffectedUser column" HeaderText="AffectedUser" SortExpression="AffectedUser" UniqueName="AffectedUser">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="AssignedTo" FilterControlAltText="Filter AssignedTo column" HeaderText="AssignedTo" SortExpression="AssignedTo" UniqueName="AssignedTo">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="Owner" FilterControlAltText="Filter Owner column" HeaderText="Owner" SortExpression="Owner" UniqueName="Owner">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="Location" FilterControlAltText="Filter Location column" HeaderText="Location" SortExpression="Location" UniqueName="Location">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="Resolution" FilterControlAltText="Filter Resolution column" HeaderText="Resolution" SortExpression="Resolution" UniqueName="Resolution">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="Close Date" DataType="System.DateTime" FilterControlAltText="Filter CloseDate column" HeaderText="Close Date" SortExpression="CloseDate" UniqueName="CloseDate">
                </telerik:GridBoundColumn>
            </Columns>
        </MasterTableView>

<FilterMenu RenderMode="Lightweight"></FilterMenu>

<HeaderContextMenu RenderMode="Lightweight"></HeaderContextMenu>
</telerik:RadGrid>

enter image description here


Solution

  • Addressed the issue with a different approach.

    • Changed to HyperLinkColumn
    • Used it as follow DataNavigateUrlFormatString="~\filedownload_6?FileName={0}"
    • Added a "Download Page"
    • Created a Virtual Directory in the website in IIS where the app is running which points to File Share and used it in the Server.MapPath Method. (~/Files/ in the code)

    https://support.microsoft.com/en-ca/help/308150/how-to-create-a-virtual-directory-on-an-existing-web-site-to-a-folder

    File Download Page code:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace DBSearchSolution
    {
        public partial class filedownload_6 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                //string filename = Request.QueryString["29215_1_0_image001.png"].ToString();
                string filename = Request.QueryString["FileName"].ToString();
                Response.ContentType = "application/octet-steam";
                Response.AppendHeader("content-disposition", "attachment; filename=" + filename);
                Response.TransmitFile( Server.MapPath("~/Files/" + filename));
                Response.End();
            }
    
    
        }
    }