In this article I will explain with an example, how to upload files, save in Folder (Directory) on Server’s Disk and display in GridView in ASP.Net using C# and VB.Net.
The uploaded Files can be deleted and downloaded from Folder (Directory) in ASP.Net.
 
 
HTML Markup
The following HTML Markup consists of an ASP.Net GridView, a FileUpload control and a Button.
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="UploadFile" />
<hr />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" EmptyDataText = "No files uploaded">
    <Columns>
        <asp:BoundField DataField="Text" HeaderText="File Name" />
        <asp:TemplateField>
            <ItemTemplate>
                <asp:LinkButton ID="lnkDownload" Text = "Download" CommandArgument = '<%# Eval("Value") %>' runat="server" OnClick = "DownloadFile"></asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:LinkButton ID = "lnkDelete" Text = "Delete" CommandArgument = '<%# Eval("Value") %>' runat = "server" OnClick = "DeleteFile" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
 
 
Namespaces
You will need to import the following namespace.
C#
using System.IO;
 
VB.Net
Imports System.IO
 
 
Uploading the File and saving in Folder (Directory) on Server’s Disk
When the File is selected in the FileUpload control and the Upload Button is clicked the following event handler is executed.
The uploaded File is saved in a Folder (Directory) named Uploads and the Page is redirected to itself in order to display the uploaded file in the GridView.
C#
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);
}
 
VB.Net
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
 
 
Displaying the Files from Folder (Directory) in ASP.Net GridView
Inside the Page Load event, the list of all files in the Uploads folder is fetched into a String Array using the GetFiles method of the Directory class.
Later, the values of the Array are copied to a Generic List collection consisting objects of ListItem class and then it is used to populate the GridView control.
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));
        }
        GridView1.DataSource = files;
        GridView1.DataBind();
    }
}
 
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
        GridView1.DataSource = files
        GridView1.DataBind()
    End If
End Sub
 
 
Downloading the Uploaded File
When the Download Button in the GridView Row is clicked, the following event handler is executed.
The Path of the File is fetched from the CommandArgument property of the LinkButton raised the event.
Finally, using the WriteFile method of the Response class, the File is written to the Response Stream using its Path and File is downloaded.
C#
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();
}
 
VB.Net
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
 
 
Deleting the Uploaded File
When the Delete Button in the GridView Row is clicked, the following event handler is executed.
The Path of the File is fetched from the CommandArgument property of the LinkButton raised the event.
Finally, using the Delete method of the File class, the File is deleted.
C#
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 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
Upload files, save in folder and display in ASP.Net GridView with Download and Delete option
 
 
Demo
 
 
Downloads