Skip to main content

connect ftp with gridview / display files in gridview from ftp or server

HTML Markup
Below is the HTML Markup of the page, where I have an ASP.Net control FileUpload to upload files, a Buttoncontrol to trigger file uploads and an ASP.NetGridViewcontrol to display the files from folder.
<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;
VB.Net
Imports 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);
}
VB.Net
Protected Sub UploadFile(ByVal sender As ObjectByVal e As EventArgs)
    Dim fileName As String = Path.GetFileName(FileUpload1.PostedFile.FileName)
    FileUpload1.PostedFile.SaveAs((Server.MapPath("~/Uploads/") + fileName))
    Response.Redirect(Request.Url.AbsoluteUri)
End Sub
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();
    }
}
VB.Net
Protected Sub Page_Load(ByVal sender As ObjectByVal e As EventArgsHandles Me.Load
    If Not IsPostBack Then
        Dim filePaths() As String = Directory.GetFiles(Server.MapPath("~/Uploads/"))
        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
        GridView1.DataSource = files
        GridView1.DataBind()
    End If
End Sub
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();
}
VB.Net
Protected Sub DownloadFile(ByVal sender As ObjectByVal e As EventArgs)
    Dim filePath As String = CType(sender, LinkButton).CommandArgument
    Response.ContentType = ContentType
    Response.AppendHeader("Content-Disposition", ("attachment; filename=" + Path.GetFileName(filePath)))
    Response.WriteFile(filePath)
    Response.End()
End Sub
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);
}
VB.Net
Protected Sub DeleteFile(ByVal sender As ObjectByVal e As EventArgs)
    Dim filePath As String = CType(sender, LinkButton).CommandArgument
    File.Delete(filePath)
    Response.Redirect(Request.Url.AbsoluteUri)
End Sub
Demo
Downloads


Comments

Popular posts from this blog

10 jQuery Custom Scrollbar Plugins

10 jQuery Custom Scrollbar Plugins If you ever wanted to add some custom scrollbars to your website, to scroll the contents and the default browser scrollbars just doesn’t match up with your design, than make sure you check this list of 10 jQuery custom scrollbar plugins. Hope you find the following information helpful. 1. jScrollPane – custom cross-browser scrollbars Kelvin Luck’s jScrollPane was originally developed in December 2006. It is a jQuery plugin which provides you with custom scrollbars which work consistently across all modern browsers. You can style the scrollbars using simple CSS and they degrade gracefully where JavaScript is disabled. 2. Plugin JQuery : Scrollbar This page is written in french so use Google’s translate service to translate this page to your preferred language. Download is available for the plugin.  The purpose of this plugin is to add a scrollbar to the item of your choice, to view any content which is larger than the size – vizibl...

Step by step installation guide for SQL Server 2012

I have so exciting news! Microsoft has released  SQL Server 2012  RTM (Code name  “Denali” ) on March 6 for manufacturing and download the evaluation edition from  http://www.microsoft.com/download/en/details.aspx?id=29066 As my laptop is 32-bit system, so I have downloaded the following files from the above link. Likewise, you can download the files for 64-bit system(x64) too. :) Finally, check your system requirements from the same link. ENU\x86\SQLFULL_x86_ENU_Core.box ENU\x86\SQLFULL_x86_ENU_Install.exe ENU\x86\SQLFULL_x86_ENU_Lang.box After downloading the above files, your system will look like below: Double click the  “SQLFULL_x86_ENU_Install.exe” , it will extract the required files for installation in the “SQLFULL_x86_ENU”  folder as shown below: Click the  “SQLFULL_x86_ENU”  folder and double click  “SETUP”  application. Checking your system requirements for installation. When you see  “SQL Server Installation Center” ...