ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Upload and Download files from Folder (Directory) in ListView using C# and VB.Net in ASP.Net<p>Hi <a class="username" rel="sani.ss501"> sani.ss501</a>,</p> <p>Check this example. Now please take its reference and correct your code.</p> <p><strong><span style="text-decoration: underline;">HTML</span></strong></p> <pre class="brush: html">&lt;asp:FileUpload ID=&#34;FileUpload1&#34; runat=&#34;server&#34; /&gt; &lt;asp:Button ID=&#34;btnUpload&#34; runat=&#34;server&#34; Text=&#34;Upload&#34; OnClick=&#34;UploadFile&#34; /&gt; &lt;hr /&gt; &lt;asp:ListView ID=&#34;ListView1&#34; runat=&#34;server&#34; GroupPlaceholderID=&#34;groupPlaceHolder1&#34; ItemPlaceholderID=&#34;itemPlaceHolder1&#34;&gt; &lt;LayoutTemplate&gt; &lt;table&gt; &lt;tr&gt; &lt;th&gt;File Name&lt;/th&gt; &lt;th&gt;&lt;/th&gt; &lt;th&gt;&lt;/th&gt; &lt;/tr&gt; &lt;asp:PlaceHolder runat=&#34;server&#34; ID=&#34;groupPlaceHolder1&#34;&gt;&lt;/asp:PlaceHolder&gt; &lt;/table&gt; &lt;/LayoutTemplate&gt; &lt;GroupTemplate&gt; &lt;tr&gt;&lt;asp:PlaceHolder runat=&#34;server&#34; ID=&#34;itemPlaceHolder1&#34;&gt;&lt;/asp:PlaceHolder&gt;&lt;/tr&gt; &lt;/GroupTemplate&gt; &lt;ItemTemplate&gt; &lt;td&gt;&lt;%# Eval(&#34;Text&#34;)%&gt;&lt;/td&gt; &lt;td&gt; &lt;asp:LinkButton ID=&#34;lnkDownload&#34; Text=&#34;Download&#34; CommandArgument=&#39;&lt;%# Eval(&#34;Value&#34;) %&gt;&#39; runat=&#34;server&#34; OnClick=&#34;DownloadFile&#34;&gt;&lt;/asp:LinkButton&gt; &lt;/td&gt; &lt;td&gt; &lt;asp:LinkButton ID=&#34;lnkDelete&#34; Text=&#34;Delete&#34; CommandArgument=&#39;&lt;%# Eval(&#34;Value&#34;) %&gt;&#39; runat=&#34;server&#34; OnClick=&#34;DeleteFile&#34; /&gt; &lt;/td&gt; &lt;/ItemTemplate&gt; &lt;/asp:ListView&gt;</pre> <p><strong><span style="text-decoration: underline;">Namespaces</span></strong></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using System.IO;</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.IO</pre> <p><strong><span style="text-decoration: underline;">Code</span></strong></p> <p><strong>C#</strong></p> <pre class="brush: csharp">protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { string[] filePaths = Directory.GetFiles(Server.MapPath(&#34;~/Uploads/&#34;)); List&lt;ListItem&gt; files = new List&lt;ListItem&gt;(); foreach (string filePath in filePaths) { files.Add(new ListItem(Path.GetFileName(filePath), filePath)); } ListView1.DataSource = files; ListView1.DataBind(); } } protected void UploadFile(object sender, EventArgs e) { string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName); FileUpload1.PostedFile.SaveAs(Server.MapPath(&#34;~/Uploads/&#34;) + fileName); Response.Redirect(Request.Url.AbsoluteUri); } protected void DownloadFile(object sender, EventArgs e) { string filePath = (sender as LinkButton).CommandArgument; Response.ContentType = ContentType; Response.AppendHeader(&#34;Content-Disposition&#34;, &#34;attachment; filename=&#34; + Path.GetFileName(filePath)); Response.WriteFile(filePath); Response.End(); } protected void DeleteFile(object sender, EventArgs e) { string filePath = (sender as LinkButton).CommandArgument; File.Delete(filePath); Response.Redirect(Request.Url.AbsoluteUri); }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load If Not IsPostBack Then Dim filePaths() As String = Directory.GetFiles(Server.MapPath(&#34;~/Uploads/&#34;)) Dim files As List(Of ListItem) = New List(Of ListItem) For Each filePath As String In filePaths files.Add(New ListItem(Path.GetFileName(filePath), filePath)) Next ListView1.DataSource = files ListView1.DataBind() End If End Sub Protected Sub UploadFile(ByVal sender As Object, ByVal e As EventArgs) Dim fileName As String = Path.GetFileName(FileUpload1.PostedFile.FileName) FileUpload1.PostedFile.SaveAs((Server.MapPath(&#34;~/Uploads/&#34;) + fileName)) Response.Redirect(Request.Url.AbsoluteUri) End Sub Protected Sub DownloadFile(ByVal sender As Object, ByVal e As EventArgs) Dim filePath As String = CType(sender, LinkButton).CommandArgument Response.ContentType = ContentType Response.AppendHeader(&#34;Content-Disposition&#34;, (&#34;attachment; filename=&#34; + Path.GetFileName(filePath))) Response.WriteFile(filePath) Response.End() End Sub Protected Sub DeleteFile(ByVal sender As Object, ByVal e As EventArgs) Dim filePath As String = CType(sender, LinkButton).CommandArgument File.Delete(filePath) Response.Redirect(Request.Url.AbsoluteUri) End Sub</pre> <p><strong><span style="text-decoration: underline;">Screenshot</span></strong></p> <p><img src="https://www.aspsnippets.com/Handlers/DownloadFile.ashx?File=77f086e6-ceec-4784-822a-385fe78f1626.gif" alt="" width="419" height="199" /></p>https://www.aspforums.net:443/Threads/267857/Upload-and-Download-files-from-Folder-Directory-in-ListView-using-C-and-VBNet-in-ASPNet/https://www.aspforums.net:443/Threads/267857/Upload-and-Download-files-from-Folder-Directory-in-ListView-using-C-and-VBNet-in-ASPNet/Tue, 03 Sep 2019 03:51:44 GMT