Upload and Download files from Folder (Directory) in ListView using C# and VB.Net in ASP.Net

Last Reply 17 days ago By dharmendr

Posted 17 days ago

Hi sani.ss501,

Check this example. Now please take its reference and correct your code.

HTML

<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="UploadFile" />
<hr />
<asp:ListView ID="ListView1" runat="server" GroupPlaceholderID="groupPlaceHolder1"
    ItemPlaceholderID="itemPlaceHolder1">
    <LayoutTemplate>
        <table>
            <tr>
                <th>File Name</th>
                <th></th>
                <th></th>
            </tr>
            <asp:PlaceHolder runat="server" ID="groupPlaceHolder1"></asp:PlaceHolder>
        </table>
    </LayoutTemplate>
    <GroupTemplate>
        <tr><asp:PlaceHolder runat="server" ID="itemPlaceHolder1"></asp:PlaceHolder></tr>
    </GroupTemplate>
    <ItemTemplate>
        <td><%# Eval("Text")%></td>
        <td>
            <asp:LinkButton ID="lnkDownload" Text="Download" CommandArgument='<%# Eval("Value") %>'
                runat="server" OnClick="DownloadFile"></asp:LinkButton>
        </td>
        <td>
            <asp:LinkButton ID="lnkDelete" Text="Delete" CommandArgument='<%# Eval("Value") %>'
                runat="server" OnClick="DeleteFile" />
        </td>
    </ItemTemplate>
</asp:ListView>

Namespaces

C#

using System.IO;

VB.Net

Imports System.IO

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        string[] filePaths = Directory.GetFiles(Server.MapPath("~/Uploads/"));
        List<ListItem> files = new List<ListItem>();
        foreach (string filePath in filePaths)
        {
            files.Add(new ListItem(Path.GetFileName(filePath), filePath));
        }
        ListView1.DataSource = files;
        ListView1.DataBind();
    }
}

protected void UploadFile(object sender, EventArgs e)
{
    string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
    FileUpload1.PostedFile.SaveAs(Server.MapPath("~/Uploads/") + fileName);
    Response.Redirect(Request.Url.AbsoluteUri);
}

protected void DownloadFile(object sender, EventArgs e)
{
    string filePath = (sender as LinkButton).CommandArgument;
    Response.ContentType = ContentType;
    Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(filePath));
    Response.WriteFile(filePath);
    Response.End();
}

protected void DeleteFile(object sender, EventArgs e)
{
    string filePath = (sender as LinkButton).CommandArgument;
    File.Delete(filePath);
    Response.Redirect(Request.Url.AbsoluteUri);
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not IsPostBack Then
        Dim filePaths() As String = Directory.GetFiles(Server.MapPath("~/Uploads/"))
        Dim files As List(Of ListItem) = New List(Of ListItem)
        For Each filePath As String In filePaths
            files.Add(New ListItem(Path.GetFileName(filePath), filePath))
        Next
        ListView1.DataSource = files
        ListView1.DataBind()
    End If
End Sub

Protected Sub UploadFile(ByVal sender As Object, ByVal e As EventArgs)
    Dim fileName As String = Path.GetFileName(FileUpload1.PostedFile.FileName)
    FileUpload1.PostedFile.SaveAs((Server.MapPath("~/Uploads/") + fileName))
    Response.Redirect(Request.Url.AbsoluteUri)
End Sub

Protected Sub DownloadFile(ByVal sender As Object, ByVal e As EventArgs)
    Dim filePath As String = CType(sender, LinkButton).CommandArgument
    Response.ContentType = ContentType
    Response.AppendHeader("Content-Disposition", ("attachment; filename=" + Path.GetFileName(filePath)))
    Response.WriteFile(filePath)
    Response.End()
End Sub

Protected Sub DeleteFile(ByVal sender As Object, ByVal e As EventArgs)
    Dim filePath As String = CType(sender, LinkButton).CommandArgument
    File.Delete(filePath)
    Response.Redirect(Request.Url.AbsoluteUri)
End Sub

Screenshot