In this article I will explain with an example, how to upload files, save in folder on Server’s Disk and display in ASP.Net GridView using C# and VB.Net.
The uploaded Files can be deleted and downloaded from folder on Server’s Disk in ASP.Net.
 
 

HTML Markup

The HTML Markup consists of following controls:
FileUpload – For selecting file.
Button – For uploading selected file.
GridView – For displaying data.

Columns

The GridView consists of following columns:
BoundField – For displaying name of the File.
TemplateField – There are two TemplateField columns, each consisting of a LinkButton for Download and Delete operations of the File.
Each LinkButton has been assigned with OnClick event handler and CommandArgument property.
Note: The CommandArgument property is set with the path of the File.
 
<asp:FileUpload ID="fuUpload" runat="server" />
<asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="UploadFile" />
<hr />
<asp:GridView ID="gvFiles" 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 Upload Button is clicked, the uploaded File is saved into the Folder (Directory) 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)
{
    //Fetch the name of the File.
    string fileName = Path.GetFileName(fuUpload.PostedFile.FileName);
 
    //Save the File.
    fuUpload.PostedFile.SaveAs(Server.MapPath("~/Uploads/") + fileName);
 
    //Redirect to the same page.
    Response.Redirect(Request.Url.AbsoluteUri);
}
 
VB.Net
Protected Sub UploadFile(ByVal sender As Object, ByVal e As EventArgs)
    'Fetch the name of the File.
    Dim fileName As String = Path.GetFileName(fuUpload.PostedFile.FileName)
 
    'Save the File.
    fuUpload.PostedFile.SaveAs((Server.MapPath("~/Uploads/") + fileName))
 
    'Redirect to the same page.
    Response.Redirect(Request.Url.AbsoluteUri)
End Sub
 
 

Displaying the Files from Folder (Directory) in ASP.Net GridView

Inside the Page_Load event handler, the path of all the Files in the Uploads folder is fetched into a String Array using the GetFiles method of the Directory class.
Then, a FOR EACH loop is executed over the Array of file paths and inside the loop, the name path of the File is stored in the Text and Value property of the ListItem class object which is later added to the Generic List collection of ListItem class.
Finally, Generic List collection is assigned to the DataSource property of the GridView control.
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        //Fetching the List of Files.
        string[] filePaths = Directory.GetFiles(Server.MapPath("~/Uploads/"));
        List<ListItem> files = new List<ListItem>();
 
        //Populating List of ListItem class.
        foreach (string filePath in filePaths)
        {
            //Storing the Name and Path in Text and Value properties.
            files.Add(new ListItem()
            {
                Text = Path.GetFileName(filePath),
                Value = filePath
            });
        }
 
        gvFiles.DataSource = files;
        gvFiles.DataBind();
    }
}
 
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not IsPostBack Then
        'Fetching the List of Files.
        Dim filePaths() As String = Directory.GetFiles(Server.MapPath("~/Uploads/"))
        Dim files As List(Of ListItem) = New List(Of ListItem)
 
        'Populating List of ListItem class.
        For Each filePath As String In filePaths
            files.Add(New ListItem() With {
            .Text = Path.GetFileName(filePath),
            .Value = filePath
        })
        Next
        gvFiles.DataSource = files
        gvFiles.DataBind()
    End If
End Sub
 
 

Downloading the Uploaded File

When the Download Button is clicked, the path of the File is fetched using the CommandArgument property.
Then, the Content Type of the file is determined using GetMimeMapping method of MimeMapping class and is set in the Response.
Note: For more details on how to get Content Type of the file in ASP.Net, please refer my article Get MIME Type (Content Type) of a File in ASP.Net.
 
Then, the Content-Disposition header is added to the Response which notifies the Browser that the File being downloaded is an Attachment.
Note: If Content-Disposition header is not specified, the Browser will try to open the File. For more details on Content-Disposition header, please refer my article What is Content Disposition Header in ASP.Net.
 
Finally, using the WriteFile method of the Response class, the File is written to the Response Stream and the File is downloaded.
C#
protected void DownloadFile(object sender, EventArgs e)
{
    //Fetch the path of the File.
    string filePath = (sender as LinkButton).CommandArgument;
 
    //Determine the name of the File.
    string fileName = Path.GetFileName(filePath);
 
    //Determine and set the Content Type of the File.
    Response.ContentType = MimeMapping.GetMimeMapping(fileName);
 
    //Specifying that the file is an Attachment.
    Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
 
    //Writing the File to the Response.
    Response.WriteFile(filePath);
    Response.End();
}
 
VB.Net
Protected Sub DownloadFile(ByVal sender As Object, ByVal e As EventArgs)
    'Fetch the path of the File.
    Dim filePath As String = CType(sender, LinkButton).CommandArgument
 
    'Determine the name of the File.
    Dim fileName As String = Path.GetFileName(filePath)
 
    'Determine and set the Content Type of the File.
    Response.ContentType = MimeMapping.GetMimeMapping(fileName)
 
    'Specifying that the file is an Attachment.
    Response.AppendHeader("Content-Disposition", ("attachment; filename=" + fileName))
 
    'Writing the File to the Response.
    Response.WriteFile(filePath)
    Response.End()
End Sub
 
 

Deleting the Uploaded File

When the Delete Button is clicked, the path of the File is fetched using the CommandArgument property.
Finally, using the Delete method of the File class, the File is deleted and the Page is redirected to itself in order to refresh the GridView.
C#
protected void DeleteFile(object sender, EventArgs e)
{
    //Fetch the path of the File.
    string filePath = (sender as LinkButton).CommandArgument;
 
    //Delete the File.
    File.Delete(filePath);
 
    //Redirect to the same page.
    Response.Redirect(Request.Url.AbsoluteUri);
}
 
VB.Net
Protected Sub DeleteFile(ByVal sender As Object, ByVal e As EventArgs)
    'Fetch the path of the File.
    Dim filePath As String = CType(sender, LinkButton).CommandArgument
 
    'Delete the File.
    File.Delete(filePath)
 
    'Redirect to the same page.
    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

In this article I will explain with an example, how to upload files, save in folder on Server’s Disk and display in ASP.Net GridView using C# and VB.Net.
The uploaded Files can be deleted and downloaded from folder on Server’s Disk in ASP.Net.
 
 

HTML Markup

The HTML Markup consists of following controls:
FileUpload – For selecting file.
Button – For uploading selected file.
GridView – For displaying data.

Columns

The GridView consists of following columns:
BoundField – For displaying name of the File.
TemplateField – There are two TemplateField columns, each consisting of a LinkButton for Download and Delete operations of the File.
Each LinkButton has been assigned with OnClick event handler and CommandArgument property.
Note: The CommandArgument property is set with the path of the File.
 
<asp:FileUpload ID="fuUpload" runat="server" />
<asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="UploadFile" />
<hr />
<asp:GridView ID="gvFiles" 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 Upload Button is clicked, the uploaded File is saved into the Folder (Directory) 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)
{
    //Fetch the name of the File.
    string fileName = Path.GetFileName(fuUpload.PostedFile.FileName);
 
    //Save the File.
    fuUpload.PostedFile.SaveAs(Server.MapPath("~/Uploads/") + fileName);
 
    //Redirect to the same page.
    Response.Redirect(Request.Url.AbsoluteUri);
}
 
VB.Net
Protected Sub UploadFile(ByVal sender As Object, ByVal e As EventArgs)
    'Fetch the name of the File.
    Dim fileName As String = Path.GetFileName(fuUpload.PostedFile.FileName)
 
    'Save the File.
    fuUpload.PostedFile.SaveAs((Server.MapPath("~/Uploads/") + fileName))
 
    'Redirect to the same page.
    Response.Redirect(Request.Url.AbsoluteUri)
End Sub
 
 

Displaying the Files from Folder (Directory) in ASP.Net GridView

Inside the Page_Load event handler, the path of all the Files in the Uploads folder is fetched into a String Array using the GetFiles method of the Directory class.
Then, a FOR EACH loop is executed over the Array of file paths and inside the loop, the name path of the File is stored in the Text and Value property of the ListItem class object which is later added to the Generic List collection of ListItem class.
Finally, Generic List collection is assigned to the DataSource property of the GridView control.
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        //Fetching the List of Files.
        string[] filePaths = Directory.GetFiles(Server.MapPath("~/Uploads/"));
        List<ListItem> files = new List<ListItem>();
 
        //Populating List of ListItem class.
        foreach (string filePath in filePaths)
        {
            //Storing the Name and Path in Text and Value properties.
            files.Add(new ListItem()
            {
                Text = Path.GetFileName(filePath),
                Value = filePath
            });
        }
 
        gvFiles.DataSource = files;
        gvFiles.DataBind();
    }
}
 
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not IsPostBack Then
        'Fetching the List of Files.
        Dim filePaths() As String = Directory.GetFiles(Server.MapPath("~/Uploads/"))
        Dim files As List(Of ListItem) = New List(Of ListItem)
 
        'Populating List of ListItem class.
        For Each filePath As String In filePaths
            files.Add(New ListItem() With {
            .Text = Path.GetFileName(filePath),
            .Value = filePath
        })
        Next
        gvFiles.DataSource = files
        gvFiles.DataBind()
    End If
End Sub
 
 

Downloading the Uploaded File

When the Download Button is clicked, the path of the File is fetched using the CommandArgument property.
Then, the Content Type of the file is get using GetMimeMapping method of MimeMapping class.
Note: For more details on how to get Content Type of the file in ASP.Net, please refer my article Get MIME Type of a File in ASP.Net.
 
After that, the Response class properties are set.
Properties:
1. ContentType – It informs the Browser about the file type.
2. Content-Disposition – It is a response header indicating, the download file is an attachment and allows setting the file name.
Note: For more details on Content-Disposition, please refer my article What is Content Disposition Header in ASP.Net.
 
Once the Response properties are set, the file contents are read.
Finally, using the WriteFile method of the Response class, the File is written to the Response Stream using its path and the File is downloaded.
C#
protected void DownloadFile(object sender, EventArgs e)
{
    //Fetch the path of the File.
    string filePath = (sender as LinkButton).CommandArgument;
 
    //Determine the name of the File.
    string fileName = Path.GetFileName(filePath);
 
    //Determine and set the Content Type of the File.
    Response.ContentType = MimeMapping.GetMimeMapping(fileName);
 
    //Specifying that the file is an Attachment.
    Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
 
    //Writing the File to the Response.
    Response.WriteFile(filePath);
    Response.End();
}
 
VB.Net
Protected Sub DownloadFile(ByVal sender As Object, ByVal e As EventArgs)
    'Fetch the path of the File.
    Dim filePath As String = CType(sender, LinkButton).CommandArgument
 
    'Determine the name of the File.
    Dim fileName As String = Path.GetFileName(filePath)
 
    'Determine and set the Content Type of the File.
    Response.ContentType = MimeMapping.GetMimeMapping(fileName)
 
    'Specifying that the file is an Attachment.
    Response.AppendHeader("Content-Disposition", ("attachment; filename=" + fileName))
 
    'Writing the File to the Response.
    Response.WriteFile(filePath)
    Response.End()
End Sub
 
 

Deleting the Uploaded File

When the Delete Button is clicked, the path of the File is fetched using the CommandArgument property.
Finally, using the Delete method of the File class, the File is deleted and the Page is redirected to itself in order to refresh the GridView.
C#
protected void DeleteFile(object sender, EventArgs e)
{
    //Fetch the path of the File.
    string filePath = (sender as LinkButton).CommandArgument;
 
    //Delete the File.
    File.Delete(filePath);
 
    //Redirect to the same page.
    Response.Redirect(Request.Url.AbsoluteUri);
}
 
VB.Net
Protected Sub DeleteFile(ByVal sender As Object, ByVal e As EventArgs)
    'Fetch the path of the File.
    Dim filePath As String = CType(sender, LinkButton).CommandArgument
 
    'Delete the File.
    File.Delete(filePath)
 
    'Redirect to the same page.
    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