Introduction
In this article we will upload files using FileUpload Control and perform download and delete operations using Link Button.
Asp.Net C# provides a file upload toolbox for upload functionality.
Let's start with the output of File Upload Control.
Prerequisites
Visual Studio 2010 or above (I used Visual Studio 2017).
In this article we will learn how to upload, download, and delete data to a web server in ASP. Net C# using File Upload Control and Display in Gridview.
Step 1
Launch/Open Visual Studio -> Go to File Menu -> New -> Project.
Step 2
Select Visual C# from left side template then choose web -> ASP.Net web application and name it then click on Ok -> Select Empty tempate -> click on Ok.
Step 3
Right Click on Solution Explorer -> Add -> New Folder -> Rename New Folder to "Uploads".
Step 4
Right click on Solution Explorer -> Add -> New Item .
Step 5
Select Web Form-> Click on Add.
Step 6
Open FileGrid.aspx File and drag out file upload control, button and gridview from Toolbar in FileGrid.aspx
Step 7
In fileGrid.aspx write the following code.
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="FileGrid.aspx.cs" Inherits="FileUploading.FileGrid" %>
-
- <!DOCTYPE html>
-
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
-
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, initial-scale=1">
- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
- <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
- <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
-
-
- <title></title>
- </head>
- <body>
- <form id="form1" runat="server">
-
- <div class="container">
- <div class="page-header">
- <h3>File Uploading in ASP.NET</h3>
-
- </div>
- </div>
-
- <table>
- <thead>
-
-
- <tr>
- <th> </th>
- <th> </th>
-
- <th><asp:FileUpload ID="myFile" runat="server" CssClass="btn btn-warning" /></th>
- <th> </th>
- <th><asp:Button ID="btnUpload" Text="upload" runat="server" CssClass="btn btn-success" OnClick="uploadData"/></th>
- </tr>
-
-
-
- </thead>
- </table>
- <hr />
- <div>
- <asp:GridView ID="fileGridview" UseAccessibleHeader="true" runat="server" CssClass="table table-hover table-striped" GridLines="None" AutoGenerateColumns="false" EmptyDataText="No Files Uploaded">
- <Columns>
- <asp:BoundField DataField="Text" HeaderText="File Name"/>
- <asp:TemplateField>
- <ItemTemplate>
-
- <asp:LinkButton ID="DownloadLink" runat="server" Text="Download" OnClick="DownloadData" CommandArgument='<%# Eval("Value") %>'><img src="Image/ic_download.png" /></asp:LinkButton>
- </ItemTemplate>
-
- </asp:TemplateField>
-
- <asp:TemplateField>
- <ItemTemplate>
-
- <asp:LinkButton ID="DeleteLink" runat="server" Text="Delete" OnClick="DeleteData" CommandArgument=' <%# Eval("value") %>'><img src="Image/ic_document.png" /></asp:LinkButton>
- </ItemTemplate>
-
- </asp:TemplateField>
-
- </Columns>
-
- </asp:GridView>
- <script type="text/javascript">
-
- $(document).ready(function() {
- $('#fileGridview').DataTable();
- } );
- </script>
- </div>
- </form>
- </body>
- </html>
Design looks like the following image,
Step 8
In FileGrid.aspx.cs write the following code.
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
-
- namespace FileUploading
- {
- public partial class FileGrid : System.Web.UI.Page
- {
- 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));
- }
-
-
- fileGridview.DataSource = files;
- fileGridview.DataBind();
-
- }
- }
-
- protected void uploadData(object sender, EventArgs e)
- {
- string fileName = Path.GetFileName(myFile.PostedFile.FileName);
- myFile.PostedFile.SaveAs(Server.MapPath("~/Uploads/") + fileName);
- Response.Redirect(Request.Url.AbsoluteUri);
- }
- protected void DownloadData(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 DeleteData(object sender, EventArgs e)
- {
- string filePath = (sender as LinkButton).CommandArgument;
- File.Delete(filePath);
- Response.Redirect(Request.Url.AbsoluteUri);
- }
- }
- }
Now your project is ready to run. Press F5 Key.
Output 1 - Select Operation.
Output 2 - Upload Operation
Output 3 - Download Operation.
Output 4
Output 5 - Delete Operation
Before delete operation:
After delete operation: Document5 file is deleted successfully.
Conclusion
ASP.NET C# allows us to perform file upload operations using File Upload Control. We learned how to perform upload, delete and download operations on documents using Fileupload control and Gridview.