Click here to Skip to main content
15,886,075 members
Articles / Protected
Article

Displaying the files for a specific folder and allow the user to download them.

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
11 Oct 2013CPOL 5.1K   1  
This is how to to list the files in a specific directory and allow the user to download the files.ASPX: <asp:GridView ID="GridView1"

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

This is how to to list the files in a specific directory and allow the user to download the files.

ASPX:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="2"
    ForeColor="#333333" GridLines="None" AllowPaging="True">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:LinkButton ID="lnkdownload" runat="server" Text="Download" CommandName="Download"
                    CommandArgument='<%#Eval("FullName") +";" + Eval("Name") %>'></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>

VB:

Private Sub BindGrid()
     Dim DataDirectory As String = "~/Uploads"

     Dim files() As FileInfo = New DirectoryInfo(Server.MapPath(DataDirectory)).GetFiles
     GridView1.DataSource = files
     GridView1.DataBind()


 End Sub
 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
     If Not IsPostBack Then
         BindGrid()
     End If
 End Sub

 Private Sub Downloadfile(ByVal fileName As String, ByVal FullFilePath As String)
     Response.AddHeader("Content-Disposition", "attachment; filename=" & fileName)
     Response.TransmitFile(FullFilePath)
     Response.End()
 End Sub

 Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
     If e.CommandName = "Download" Then
         Dim fileInfo() As String = e.CommandArgument.ToString().Split(";")
         Dim FileName As String = fileInfo(1)
         Dim FullPath As String = fileInfo(0)
         Downloadfile(FileName, FullPath)
     End If
 End Sub

 Protected Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridView1.PageIndexChanging
     GridView1.PageIndex = e.NewPageIndex
     BindGrid()

 End Sub

 

Note: the mentioned code List the files from "Uploads" directory  , and so you may need to change the name of that folder based on your website, also the code can be extended to allow the user to Dynamicly selects the directory name to list its files.

 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
The ASP.NET Wiki was started by Scott Hanselman in February of 2008. The idea is that folks spend a lot of time trolling the blogs, googlinglive-searching for answers to common "How To" questions. There's piles of fantastic community-created and MSFT-created content out there, but if it's not found by a search engine and the right combination of keywords, it's often lost.

The ASP.NET Wiki articles moved to CodeProject in October 2013 and will live on, loved, protected and updated by the community.
This is a Collaborative Group

754 members

Comments and Discussions

 
-- There are no messages in this forum --