This article describes how to get the list of all files from a server folder or directory with a download link option in DataList control.

Suppose you have a folder or directory in your ASP.NET server application and you want to show all the files in that directory on the web page with a download link and allow the user to delete files from the server.

Here I will get all the fields from the server directory and display them in the DataList control with an image icon, download link and a delete option. This will work with any type of extension file.

HTML Markup for the UI

Here, I use an ASP.NET DataList control to display the files from the server directory.

  1. <body>
  2. <form id="form1" runat="server">
  3. <asp:FileUpload ID="FileUpload1" runat="server" />
  4. <asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="UploadFile" />
  5. <div style="border: solid 1px #D9DCEA; border-top: none; width: 40%; margin-top: -1px;
  6. float: right">
  7. <div style="border-top: none; width: 97%; padding: 5px;">
  8. <asp:DataList ID="DataListContent" runat="server" OnItemCommand="ButtonDownloadContent"
  9. RepeatDirection="Vertical" BorderStyle="None" Style="padding: 0px!important">
  10. <ItemTemplate>
  11. <div>
  12. <img src='<%# DataBinder.Eval(Container.DataItem,"Icon") %>' id="ImgIcon" />
  13. <asp:LinkButton ID="ButtonDownload" runat="server" Style="padding-left: 5px; text-decoration: none"
  14. ToolTip="Click here to download" CommandName="Download" CommandArgument='<%# DataBinder.Eval(Container.DataItem,"DownLoadLink") %>'
  15. Text=' <%# DataBinder.Eval(Container.DataItem,"FileName") %>'></asp:LinkButton>
  16. <asp:LinkButton ID="lnkDelete" Text="Delete" CommandArgument='<%# Eval("DownLoadLink") %>'
  17. Style="text-decoration: none; font-size: large; color: red;" runat="server" OnClick="DeleteFile" />
  18. </div>
  19. </ItemTemplate>
  20. </asp:DataList>
  21. </div>
  22. </div>
  23. </form>
  24. </body>

Getting files from directory.

Here I get all the files from the directory to show in the DataList control.

  1. private void GenerateDownloadLinks()
  2. {
  3. string path = Server.MapPath("~/UploadFile");
  4. if (Directory.Exists(path))
  5. {
  6. DataTable ShowContent = new DataTable();
  7. ShowContent.Columns.Add("Icon", typeof(string));
  8. ShowContent.Columns.Add("DownloadLink", typeof(string));
  9. ShowContent.Columns.Add("FileName", typeof(string));
  10. DirectoryInfo di = new DirectoryInfo(path);
  11. foreach (FileInfo fi in di.GetFiles())
  12. {
  13. DataRow dr = ShowContent.NewRow();
  14. dr["FileName"] = fi.Name; ;
  15. dr["DownloadLink"] = Server.MapPath("~/UploadFile/") + fi.Name;
  16. string ext = Path.GetExtension(fi.FullName);
  17. switch (ext)
  18. {
  19. case "png":
  20. dr["Icon"] = ResolveUrl("~/images/PdfIcon.png");
  21. break;
  22. case "doc":
  23. dr["Icon"] = ResolveUrl("~/images/DocIcon.png");
  24. break;
  25. case "xls":
  26. dr["Icon"] = ResolveUrl("~/images/ExcelIcon.png");
  27. break;
  28. case "txt":
  29. dr["Icon"] = ResolveUrl("~/images/TxtIcon.png");
  30. break;
  31. case "zip":
  32. dr["Icon"] = ResolveUrl("~/images/ZipIcon.png");
  33. break;
  34. }
  35. DataListContent.DataSource = ShowContent;
  36. DataListContent.DataBind();
  37. }
  38. }
  39. }

Code for downloading the file:

  1. protected void ButtonDownloadContent(object sender, DataListCommandEventArgs e)
  2. {
  3. if (e.CommandName == "Download")
  4. {
  5. string path = e.CommandArgument.ToString();
  6. string name = Path.GetFileName(path);
  7. string ext = Path.GetExtension(path);
  8. Response.AppendHeader("content-disposition", "attachment; filename=" + name);
  9. Response.ContentType = "application/octet-stream";
  10. Response.WriteFile(path);
  11. Response.End();
  12. }
  13. }
Code for deleting a file from the directory:
  1. protected void DeleteFile(object sender, EventArgs e)
  2. {
  3. string filePath = (sender as LinkButton).CommandArgument;
  4. File.Delete(filePath);
  5. GenerateDownloadLinks();
  6. }

Code for uploading a file from the directory:

  1. protected void UploadFile(object sender, EventArgs e)
  2. {
  3. string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
  4. FileUpload1.PostedFile.SaveAs(Server.MapPath("~/UploadFile/") + fileName);
  5. Response.Redirect(Request.Url.AbsoluteUri);
  6. }

download-file-in-asp.net.png