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 – vizible sp

Working with Ajax UpdatePanel in asp.net

During work with our applications if we entered any values in textbox controls and click on a button in form we will see full postback of our page and we will lost all the controls values whatever we entered previously this happend because of postback. If we want to avoid this full postback of page and round trip to server we need to write much code instead of writing much code we can use ajax updatepanel control. Ajax updatepanel will help us to avoid full postback of the page i.e., avoid refresh of the whole page content with postback and stop flickering of the page which is associated with a postback and allows only partial postbacks. By using Ajax updatepanel we can refresh only required part of page instead of refreshing whole page. Ajax updatepanel works on very smooth concepts. but in some cases we got stuck while using Ajax updatepanel. But you need not to worry. Here i am showing you how to resolve the issues for different cases Lets start with very basic use of A