Function to download a file from a folder

5 posts, 1 answers
  1. Jaime
    Jaime avatar
    8 posts
    Member since:
    Dec 2012

    Posted 06 Dec 2012 Link to this post

    I found an example but with a gridview, can anyone help me to use it with RadGrid???


    protected void lnkDownload_Click(object sender, EventArgs e)
        {
            LinkButton lnkbtn = sender as LinkButton;
            GridViewRow gvrow = lnkbtn.NamingContainer as GridViewRow;
            string filePath = gvDetails.DataKeys[gvrow.RowIndex].Value.ToString();
            Response.ContentType = "application/vnd.ms-excel, application/pdf, application/vnd.ms-excel";
            Response.AddHeader("Content-Disposition", "attachment;filename=\"" + filePath + "\"");
            Response.TransmitFile(Server.MapPath(filePath));
            Response.End();
        }


    where gvDetails is a Gridview.

    Thanks.
  2. Answer
    Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 06 Dec 2012 Link to this post

    Hi,

    Please try the following code snippet.

    C#:
    protected void lnkDownload_Click(object sender, EventArgs e)
    {
        LinkButton lnkbtn = sender as LinkButton;
        GridDataItem item = (GridDataItem)lnkbtn.NamingContainer;
        string filePath = item.OwnerTableView.DataKeyValues[item.ItemIndex]["DataKeyName"].ToString();
        Response.ContentType = "application/vnd.ms-excel, application/pdf, application/vnd.ms-excel";
        Response.AddHeader("Content-Disposition", "attachment;filename=\"" + filePath + "\"");
        Response.TransmitFile(Server.MapPath(filePath));
        Response.End();
    }


    Thanks,
    Princy.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Jaime
    Jaime avatar
    8 posts
    Member since:
    Dec 2012

    Posted 06 Dec 2012 Link to this post

    Thanks a lot!!!!
  5. Mayur
    Mayur avatar
    12 posts
    Member since:
    Oct 2013

    Posted 20 Dec 2013 Link to this post

    Hi Princy,
                   Hope you would be doing well. I am having an issue in TelerikRad Grid. I am having a TelerikRadAsynchUpload and RadBinaryImage inside a Telerikgrid. What i have to do is to upload and display a thumbnail of that Image/PDF/TXT or whatever may be the file type in RadBinaryImage after download.

    I have added a CustomHandler in RadGrid for uploading. Now the problem is when i remove CustomHandler uploading don't happens and when i put CustomHandler then the image preview not works.....

    What i have to do is both should work. Please find attached code snippet along with screenshot of issue below.

    Awaiting for your prompt reply. Looking forward to speak with you.

    Code Snippet:

    aspx
    ------
    <MasterTableView Width="100%" CommandItemDisplay="Top" DataKeyNames="CD_SRNO" CommandItemSettings-AddNewRecordText="Add New File">
                                    <Columns>
                                        <telerik:GridTemplateColumn HeaderText="Remarks" UniqueName="Description" DataField="Description">
                                            <ItemTemplate>
                                                <asp:Label ID="lblDescription" Width="400px" runat="server" Text='<%#Eval("Description") %>'></asp:Label>
                                            </ItemTemplate>
                                            <EditItemTemplate>
                                                <telerik:RadTextBox ID="txbDescription" Width="400px" runat="server" TextMode="MultiLine"
                                                    Text='<%# Eval("Description") %>' Height="30px">
                                                </telerik:RadTextBox>
                                                <asp:RequiredFieldValidator ID="Requiredfieldvalidator1" runat="server" ControlToValidate="txbDescription"
                                                    ErrorMessage="*" Display="Dynamic" SetFocusOnError="true"></asp:RequiredFieldValidator>
                                            </EditItemTemplate>
                                            <ItemStyle VerticalAlign="Top"></ItemStyle>
                                        </telerik:GridTemplateColumn>
                                        <telerik:GridTemplateColumn DataField="Data" HeaderText="Document" UniqueName="Upload">
                                            <ItemTemplate>
                                                <telerik:RadBinaryImage runat="server" ID="RadBinaryImage1" AutoAdjustImageControlSize="false"
                                                    DataValue='<%#Eval("Data") %>' Height="42px" Width="80px"></telerik:RadBinaryImage>
                                            </ItemTemplate>
                                            <EditItemTemplate>
                                                <telerik:RadAsyncUpload runat="server" ID="AsyncUpload1" AllowedFileExtensions=".jpg,.jpeg,.png,.gif,.Pdf,.doc,.docx,.xls"
                                                    MaxFileInputsCount="1" Skin="Outlook" PostbackTriggers="PerformInsertButton"
                                                    MaxFileSize="1048576" OnClientFileUploaded="OnClientFileUploaded" HttpHandlerUrl="~/Conference/CustomHandler.ashx" />
                                                <asp:CustomValidator runat="server" ID="CustomValidator" Display="Dynamic" ClientValidationFunction="validateUpload"
                                                    ErrorMessage="*" />
                                            </EditItemTemplate>
                                        </telerik:GridTemplateColumn>
                                        <telerik:GridTemplateColumn DataField="Data" HeaderText="Download" UniqueName="Upload">
                                            <ItemTemplate>
                                                <asp:LinkButton runat="server" OnClick="lnkDownload_Click" CommandArgument='<%# Eval("CD_SRNO") %>'
                                                    ID="lnkDownload" Text='<%# string.Concat("Document-", Eval("CD_SRNO"),Eval("DOCTYPE"))%>'></asp:LinkButton>
                                            </ItemTemplate>
                                        </telerik:GridTemplateColumn>
                                        <telerik:GridButtonColumn Text="Delete" CommandName="Delete" ButtonType="ImageButton">
                                            <HeaderStyle Width="36px"></HeaderStyle>
                                        </telerik:GridButtonColumn>
                                    </Columns>
                                    <EditFormSettings>
                                        <EditColumn ButtonType="ImageButton">
                                        </EditColumn>
                                    </EditFormSettings>
                                </MasterTableView> 

    CustomHandler
    -------------------

    Imports System
    Imports System.Web
    Imports Telerik.Web.UI


    Public Class CustomHandler
        Inherits AsyncUploadHandler
        
        Protected Overrides Function Process(file As UploadedFile, context As HttpContext, configuration As IAsyncUploadConfiguration, tempFileName As String) As IAsyncUploadResult
            configuration.TimeToLive = TimeSpan.FromHours(4)
            Return MyBase.Process(file, context, configuration, tempFileName)
        End Function

    End Class
  6. Hristo Valyavicharski
    Admin
    Hristo Valyavicharski avatar
    975 posts

    Posted 24 Dec 2013 Link to this post

    Hi Mayur,

    What are you doing inside the OnClientFileUploaded event? As far as I see you don't need this custom handler, because it doesn't provide additional functionality.

    Regards,
    Hristo Valyavicharski
    Telerik
    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 the blog feed now.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017