Our Products:   CompleteFTP  edtFTPnet/Free  edtFTPnet/PRO  edtFTPj/Free  edtFTPj/PRO
0 votes
4k views
in .NET FTP by (51.6k points)
A trial user writes:
...for my use I know the file I am uploading, and all of the connection information. I want the status bar information and behavior just as when a file is uploaded using the sample ftp client but I want to do the uploading and setting of properties via code and only have one button to continue and a second button to cancel the upload.

Do you have any examples of how to do FTP upload with code where the FTPStatusbar on the form functions showing progress of the upload and all file information of the file being uploaded in c# code?


- Hans (EnterpriseDT)

1 Answer

0 votes
by (51.6k points)
  1. Install the edtFTPnet/Express trial or the edtFTPnet/PRO trial.
  2. Create a new Windows Forms application in Visual Studio or Visual C#.
  3. Drag an ExFTPConnection, a FTPStatusBar and two buttons onto the form. (Note: If you are using Visual C# then you'll need to add the controls to the toolbox first - see here).
  4. Change the names of the buttons to "upload" and "cancel".
  5. Add Click event-handlers for both buttons (you can do this by double-clicking in each of them in the Designer)
  6. Replace the code in Form1.cs with the code shown below.
  7. Change the strings in the constructor to match your server (i.e. "your-server-address", "your-username", etc)
  8. Run it.

using System;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private string localDir;
        private string fileName;

        public Form1()
        {
            InitializeComponent();

            // set up connection
            exFTPConnection1.ServerAddress = "your-server-address";
            exFTPConnection1.UserName = "your-username";
            exFTPConnection1.Password = "your-password";
            exFTPConnection1.ServerDirectory = "your-server-directory";

            localDir = "your-local-directory";
            fileName = "your-file-name";
        }

        private void upload_Click(object sender, EventArgs e)
        {
            upload.Enabled = false;
            cancel.Enabled = true;

            // start connecting.  
            // OnConnected will be called when connection attempt completes (whether successfully or not)
            exFTPConnection1.BeginConnect(new AsyncCallback(OnConnected), null);
        }

        private void OnConnected(IAsyncResult res)
        {
            try
            {
                // EndConnect will report any errors that occurred while connecting
                exFTPConnection1.EndConnect(res);

                // start uploading file.
                // OnUploaded will be called when upload attempt completes (whether successfully or not)
                string localPath = System.IO.Path.Combine(localDir, fileName);
                exFTPConnection1.BeginUploadFile(localPath, fileName, new AsyncCallback(OnUploaded), null);
            }
            catch (Exception ex)
            {
                upload.Enabled = true;
                cancel.Enabled = false;
                MessageBox.Show(ex.ToString());
            }
        }

        private void OnUploaded(IAsyncResult res)
        {
            try
            {
                // EndConnect will report any errors that occurred while connecting
                exFTPConnection1.EndUploadFile(res);
                MessageBox.Show("Upload complete");
            }
            catch (EnterpriseDT.Net.Ftp.FTPTransferCancelledException)
            {
                MessageBox.Show("Upload aborted by user");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

            // start closing connection
            // OnClosed will be called when closed
            exFTPConnection1.BeginClose(new AsyncCallback(OnClosed), null);
        }

        private void OnClosed(IAsyncResult res)
        {
            // we don't call exFTPConnection1.EndClose(res) since don't really care 
            // if a problem occurred while closing since the connection 
            // will always be closed anyway
            upload.Enabled = true;
            cancel.Enabled = false;
        }

        private void cancel_Click(object sender, EventArgs e)
        {
            exFTPConnection1.CancelTransfer();
        }
    }
}


- Hans (EnterpriseDT)

Categories

...