File Uploading In ASP.NET C# Using Gridview

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.
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="FileGrid.aspx.cs" Inherits="FileUploading.FileGrid" %>    
  2.     
  3. <!DOCTYPE html>    
  4.     
  5. <html xmlns="http://www.w3.org/1999/xhtml">    
  6. <head runat="server">    
  7.     
  8.     <meta charset="utf-8">    
  9.   <meta name="viewport" content="width=device-width, initial-scale=1">    
  10.   <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">    
  11.   <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>    
  12.   <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>    
  13.         
  14.     
  15.     <title></title>    
  16. </head>    
  17. <body>    
  18.     <form id="form1" runat="server">    
  19.     
  20.         <div class="container">    
  21.             <div class="page-header">    
  22.                 <h3>File Uploading in ASP.NET</h3>    
  23.     
  24.             </div>    
  25.         </div>    
  26.     
  27.         <table>    
  28.             <thead>    
  29.                     
  30.                     
  31.                     <tr>    
  32.                         <th>               </th>    
  33.                         <th>               </th>    
  34.                             
  35.                         <th><asp:FileUpload ID="myFile" runat="server" CssClass="btn btn-warning" /></th>    
  36.                         <th>               </th>    
  37.                         <th><asp:Button  ID="btnUpload" Text="upload" runat="server" CssClass="btn btn-success"  OnClick="uploadData"/></th>    
  38.                     </tr>    
  39.            
  40.     
  41.                     
  42.             </thead>    
  43.          </table>    
  44.         <hr />    
  45.         <div>    
  46.             <asp:GridView ID="fileGridview" UseAccessibleHeader="true" runat="server" CssClass="table table-hover table-striped" GridLines="None" AutoGenerateColumns="false" EmptyDataText="No Files Uploaded">    
  47.                 <Columns>    
  48.                     <asp:BoundField DataField="Text" HeaderText="File Name"/>    
  49.                      <asp:TemplateField>    
  50.                         <ItemTemplate>    
  51.     
  52.                             <asp:LinkButton ID="DownloadLink" runat="server" Text="Download" OnClick="DownloadData" CommandArgument='<%# Eval("Value") %>'><img src="Image/ic_download.png" /></asp:LinkButton>    
  53.                         </ItemTemplate>    
  54.     
  55.                     </asp:TemplateField>    
  56.     
  57.                     <asp:TemplateField>    
  58.                         <ItemTemplate>    
  59.     
  60.                             <asp:LinkButton ID="DeleteLink" runat="server" Text="Delete" OnClick="DeleteData" CommandArgument=' <%# Eval("value") %>'><img src="Image/ic_document.png" /></asp:LinkButton>    
  61.                         </ItemTemplate>    
  62.     
  63.                     </asp:TemplateField>    
  64.     
  65.                 </Columns>    
  66.     
  67.             </asp:GridView>    
  68.             <script type="text/javascript">    
  69.     
  70.             $(document).ready(function() {    
  71.                                         $('#fileGridview').DataTable();    
  72.                                          } );    
  73.             </script>    
  74.         </div>    
  75.     </form>    
  76. </body>    
  77. </html>     
Design looks like the following image,
Step 8
 
In FileGrid.aspx.cs write the following code.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.IO;  
  4. using System.Linq;  
  5. using System.Web;  
  6. using System.Web.UI;  
  7. using System.Web.UI.WebControls;  
  8.   
  9. namespace FileUploading  
  10. {  
  11.     public partial class FileGrid : System.Web.UI.Page  
  12.     {  
  13.         protected void Page_Load(object sender, EventArgs e)  
  14.         {  
  15.             if (!IsPostBack)  
  16.             {  
  17.                 string[] filePaths = Directory.GetFiles(Server.MapPath("~/Uploads/"));  
  18.                 List<ListItem> files = new List<ListItem>();  
  19.                 foreach (string filePath in filePaths)  
  20.                 {  
  21.                     files.Add(new ListItem(Path.GetFileName(filePath), filePath));  
  22.                 }  
  23.                  
  24.                   
  25.                 fileGridview.DataSource = files;  
  26.                 fileGridview.DataBind();  
  27.              
  28.             }  
  29.         }  
  30.   
  31.         protected void uploadData(object sender, EventArgs e)  
  32.         {  
  33.             string fileName = Path.GetFileName(myFile.PostedFile.FileName);  
  34.             myFile.PostedFile.SaveAs(Server.MapPath("~/Uploads/") + fileName);  
  35.             Response.Redirect(Request.Url.AbsoluteUri);  
  36.         }  
  37.         protected void DownloadData(object sender, EventArgs e)  
  38.         {  
  39.             string filePath = (sender as LinkButton).CommandArgument;  
  40.             Response.ContentType = ContentType;  
  41.             Response.AppendHeader("Content-Disposition""attachment; filename=" + Path.GetFileName(filePath));  
  42.             Response.WriteFile(filePath);  
  43.             Response.End();  
  44.         }  
  45.         protected void DeleteData(object sender, EventArgs e)  
  46.         {  
  47.             string filePath = (sender as LinkButton).CommandArgument;  
  48.             File.Delete(filePath);  
  49.             Response.Redirect(Request.Url.AbsoluteUri);  
  50.         }  
  51.     }  
  52. }  
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.


Similar Articles