Our Products:   CompleteFTP  edtFTPnet/Free  edtFTPnet/PRO  edtFTPj/Free  edtFTPj/PRO
0 votes
2.9k views
in .NET FTP by (51.2k points)
A user asks if edtFTPnet can be used to build a browser-based FTP client. In other words, can it be used such that the user has access to the files on an FTP server through a web-page?

The answer of course is yes and no. edtFTPnet can't be used to FTP directly from your browser, for that you'd need Integral FTP. But you can use edtFTPnet to emulate an FTP client by having your server perform the FTP operations for you and then allowing the browser to stream them from your server using HTTP.

We wrote a rudimentary client in order to illustrate the concepts. It has a single page which has three text-boxes: txtServer (FTP server's host-name), txtUserName (user-name on FTP server) and txtPassword (password on FTP server); one button, btnListFiles, for listing the files on the FTP server; one list-box, lstFiles, for displaying the files; and one button, btnDownload, for downloading the selected file.

Here's the ASPX source:
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="lblServer" runat="server" Text="Server"></asp:Label>
        <asp:TextBox ID="txtServer" runat="server"></asp:TextBox><br />
        <asp:Label ID="lblUserName" runat="server" Text="UserName"></asp:Label>
        <asp:TextBox ID="txtUserName" runat="server"></asp:TextBox><br />
        <asp:Label ID="lblPassword" runat="server" Text="Password"></asp:Label>
        <asp:TextBox ID="txtPassword" runat="server"></asp:TextBox><br />
        <asp:Button ID="btnListFiles" runat="server" OnClick="btnListFiles_Click" Text="Connect" /><br />
        <asp:ListBox ID="lstFiles" runat="server"></asp:ListBox>
        <br />
        <asp:Button ID="btnDownload" runat="server" OnClick="btnDownload_Click" Text="Download" /></div>
    </form>
</body>
</html>


and here's the ASPX.CS code:
using System;
using EnterpriseDT.Net.Ftp;

public partial class _Default : System.Web.UI.Page 
{
    protected void btnListFiles_Click(object sender, EventArgs e)
    {
        FTPConnection ftpCxn = new FTPConnection();
        ftpCxn.ServerAddress = txtServer.Text;
        ftpCxn.UserName = txtUserName.Text;
        ftpCxn.Password = txtPassword.Text;
        ftpCxn.Connect();
        foreach (string fileName in ftpCxn.GetFiles())
            lstFiles.Items.Add(fileName);
        ftpCxn.Close();
    }

    protected void btnDownload_Click(object sender, EventArgs e)
    {
        Response.Clear();
        Response.ContentType = "application/octet-stream";

        FTPConnection ftpCxn = new FTPConnection();
        ftpCxn.ServerAddress = txtServer.Text;
        ftpCxn.UserName = txtUserName.Text;
        ftpCxn.Password = txtPassword.Text;
        ftpCxn.Connect();
        ftpCxn.CloseStreamsAfterTransfer = false;
        ftpCxn.DownloadStream(Response.OutputStream, lstFiles.SelectedValue);
        ftpCxn.Close();

        Response.End();
    }
}


This illustrates only downloads. The ASP.NET control, FileUpload, would be used to do uploads.

- Hans (EDT)

Please log in or register to answer this question.

Categories

...