Wednesday, September 12, 2012

Download Files from a specific folder

If you want to allow the user to download files from a specific folder in the server the following snippet of code lets you accomplish the task. Initially display all the files the user want to download in a Gridview. Put a download link to let the user download files.

HTML Markup
<div>
        <asp:GridView ID="gvFiles" runat="server" AutoGenerateColumns="False" CellPadding="2"
            ForeColor="#333333" GridLines="None" AllowPaging="True"
            onpageindexchanging="gvFiles_PageIndexChanging"
            onrowcommand="gvFiles_RowCommand">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:LinkButton ID="lbDownload" runat="server" Text="Download" CommandName="Download"
                            CommandArgument='<%#Eval("FullName")  %>'></asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="Name" HeaderText="File Name" />
                <asp:BoundField DataField="Length" HeaderText="Size (Bytes)" />
            </Columns>
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <EditRowStyle BackColor="#999999" />
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        </asp:GridView>
    </div>

C#
using System.IO;


protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            BindDownloadFiles();
        }
    }
    private void BindDownloadFiles()
    {
        string DataDirectory= "~/DownloadFiles";

        DirectoryInfo dirInfo = new DirectoryInfo(Server.MapPath(DataDirectory));
        FileInfo[] files = dirInfo.GetFiles();
        gvFiles.DataSource = files;
        gvFiles.DataBind();
    }
    private void DownloadFile(string fileName, string fullFilePath)
    {
        Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
        Response.TransmitFile(fullFilePath);
        Response.End();
    }
    protected void gvFiles_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Download")
        {
            Uri uri = new Uri(e.CommandArgument.ToString());
            string[] fileInfo = uri.LocalPath.Split(';');
            string fullPath = fileInfo[0];
            string fileName = fileInfo[1];
            DownloadFile(fileName, fullPath);
        }
    }
    protected void gvFiles_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvFiles.PageIndex = e.NewPageIndex;
        BindDownloadFiles();
    }

No comments:

Codeigniter Shield Authorization

Codeigniter Shield Authorization CodeIgniter Shield is the official authentication and authorization framework for CodeIgniter 4. It provide...