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.
- <body>
- <form id="form1" runat="server">
- <asp:FileUpload ID="FileUpload1" runat="server" />
- <asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="UploadFile" />
- <div style="border: solid 1px #D9DCEA; border-top: none; width: 40%; margin-top: -1px;
- float: right">
- <div style="border-top: none; width: 97%; padding: 5px;">
- <asp:DataList ID="DataListContent" runat="server" OnItemCommand="ButtonDownloadContent"
- RepeatDirection="Vertical" BorderStyle="None" Style="padding: 0px!important">
- <ItemTemplate>
- <div>
- <img src='<%# DataBinder.Eval(Container.DataItem,"Icon") %>' id="ImgIcon" />
- <asp:LinkButton ID="ButtonDownload" runat="server" Style="padding-left: 5px; text-decoration: none"
- ToolTip="Click here to download" CommandName="Download" CommandArgument='<%# DataBinder.Eval(Container.DataItem,"DownLoadLink") %>'
- Text=' <%# DataBinder.Eval(Container.DataItem,"FileName") %>'></asp:LinkButton>
- <asp:LinkButton ID="lnkDelete" Text="Delete" CommandArgument='<%# Eval("DownLoadLink") %>'
- Style="text-decoration: none; font-size: large; color: red;" runat="server" OnClick="DeleteFile" />
- </div>
- </ItemTemplate>
- </asp:DataList>
- </div>
- </div>
- </form>
- </body>
Getting files from directory.
Here I get all the files from the directory to show in the DataList control.
- private void GenerateDownloadLinks()
- {
- string path = Server.MapPath("~/UploadFile");
- if (Directory.Exists(path))
- {
- DataTable ShowContent = new DataTable();
- ShowContent.Columns.Add("Icon", typeof(string));
- ShowContent.Columns.Add("DownloadLink", typeof(string));
- ShowContent.Columns.Add("FileName", typeof(string));
- DirectoryInfo di = new DirectoryInfo(path);
- foreach (FileInfo fi in di.GetFiles())
- {
- DataRow dr = ShowContent.NewRow();
- dr["FileName"] = fi.Name; ;
- dr["DownloadLink"] = Server.MapPath("~/UploadFile/") + fi.Name;
- string ext = Path.GetExtension(fi.FullName);
- switch (ext)
- {
- case "png":
- dr["Icon"] = ResolveUrl("~/images/PdfIcon.png");
- break;
- case "doc":
- dr["Icon"] = ResolveUrl("~/images/DocIcon.png");
- break;
- case "xls":
- dr["Icon"] = ResolveUrl("~/images/ExcelIcon.png");
- break;
- case "txt":
- dr["Icon"] = ResolveUrl("~/images/TxtIcon.png");
- break;
- case "zip":
- dr["Icon"] = ResolveUrl("~/images/ZipIcon.png");
- break;
- }
- DataListContent.DataSource = ShowContent;
- DataListContent.DataBind();
- }
- }
- }
Code for downloading the file:
- protected void ButtonDownloadContent(object sender, DataListCommandEventArgs e)
- {
- if (e.CommandName == "Download")
- {
- string path = e.CommandArgument.ToString();
- string name = Path.GetFileName(path);
- string ext = Path.GetExtension(path);
- Response.AppendHeader("content-disposition", "attachment; filename=" + name);
- Response.ContentType = "application/octet-stream";
- Response.WriteFile(path);
- Response.End();
- }
- }
Code for deleting a file from the directory:- protected void DeleteFile(object sender, EventArgs e)
- {
- string filePath = (sender as LinkButton).CommandArgument;
- File.Delete(filePath);
- GenerateDownloadLinks();
- }
Code for uploading a file from the directory:
- protected void UploadFile(object sender, EventArgs e)
- {
- string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
- FileUpload1.PostedFile.SaveAs(Server.MapPath("~/UploadFile/") + fileName);
- Response.Redirect(Request.Url.AbsoluteUri);
- }