Monday, November 28, 2016

Download File From GridView Using ASP.Net



 <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 namespaces. C# using System.IO;


 Uploading the File and saving in directory or folder  
 When the file is selected in APS.Net FileUpload control and the Upload button is clicked the following event handler is triggered. Below I have folder Uploads in the root directory of my ASP.Net Website where I am saving the uploaded file.  
 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);  
 }  


 Displaying the files from folder or directory in ASP.Net GridView  
 Here I am reading all the files from the Uploads directory which I have created and then binding the fetched files to the ASP.Net 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();  
   }  
 }  


 Downloading the Uploaded File from ASP.Net GridView  
 The following event handler is executed when the lnkDownload LinkButton is clicked in the ASP.Net GridView Row. Using the CommandArgument property of the ASP.Net LinkButton, I get the path of the file using which I am downloading the file.  
 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();  
 }  

Deleting the Uploaded File from ASP.Net GridView  
 The following event handler is executed when the lnkDelete LinkButton is clicked in the ASP.Net GridView Row. Using the CommandArgument property of the ASP.Net LinkButton, I get the path of the file using which I am deleting the file.  
 C#  
 
 protected void DeleteFile(object sender, EventArgs e)  
 {  
   string filePath = (sender as LinkButton).CommandArgument;  
   File.Delete(filePath);  
   Response.Redirect(Request.Url.AbsoluteUri);  
 }  

No comments:

Post a Comment