EldoS | Feel safer!

Software components for data protection, secure storage and transfer

Making sure I handle FTP operation properly..

Also by EldoS: CallbackDisk
Create virtual disks backed by memory or custom location, expose disk images as disks and more.
#27378
Posted: 11/25/2013 17:01:11
by Ivan Demkovitch (Standard support level)
Joined: 11/10/2013
Posts: 21

I need to get list of files from FTP and download/save all of them, then delete on server.

Below is a snippet. I use delegate to handle OnTextDataLine event. Just wanted to make sure this is correct approach and client will wait until all files processed before it is closed.

Another question is how do I get file names reliably? Should I parse strings or there is better way? I'm getting strings that look like:

Code
drw-rw-rw-   1 user     group           0 Nov 25 18:26 .
drw-rw-rw-   1 user     group           0 Nov 25 18:26 ..
-rw-rw-rw-   1 user     group           4 Nov 25 18:26 test.txt
-rw-rw-rw-   1 user     group           0 Nov 25 18:26 test2.txt
-rw-rw-rw-   1 user     group           0 Nov 25 18:26 test3.txt



Code
// Client should be open and logged in ready for reading files
            // Handler for each file
            client.OnTextDataLine += delegate(object sender, byte[] line)
                {
                    var memoryStream = new MemoryStream();
                    var fileName = System.Text.Encoding.ASCII.GetString(line);

                    try
                    {
                        client.Receive(fileName, memoryStream);

                        // TODO: Save incoming message:

                        // TODO: Delete file on FTP
                    }
                    catch (Exception ex)
                    {
                        this.LoggerService.Log(this.AccountId, ex);
                        this.LoggerService.Log(this.AccountId, "Error downloading file: " + fileName, Category.Debug, Priority.High);
                    }
                };

            // Get list of files:
            client.GetFileList();
            if (client.Active) client.Close();
#27380
Posted: 11/25/2013 21:19:59
by Ivan Demkovitch (Standard support level)
Joined: 11/10/2013
Posts: 21

I think I figured out how to do this:


This is to connect to server (basic FTP)
Code
private TElSimpleFTPSClient GetFtpClient(EDIConnection connection)
        {
            // Connect to FTP and open in folder:
            var client = new TElSimpleFTPSClient
            {
                Address = connection.FtpUrl,
                Port = connection.FtpPort,
                Username = connection.FtpUserName,
                Password = connection.FtpPassword,
                Versions = 0,
                AuthCmd = __Global.acAuto,
                PassiveMode = true,
                UseSSL = false,
                SSLMode = SBSSLCommon.TSBSSLMode.smExplicit
            };

            try
            {
                client.Open();
                client.Login();
            }
            catch (Exception ex)
            {
                this.LoggerService.Log(this.AccountId, ex);
                return null;
            }

            return client;
        }


And this is how I use it to get files and save:

Code
var client = this.GetFtpClient(connection);
            if (client == null) return;

            client.Cwd(connection.FtpOutgoingFolder);
                        
            // Get list of files:
            var fileList = new ArrayList();
            client.GetFileList(string.Empty, fileList);
            foreach (var teiFileInfo in fileList.OfType<TElFTPFileInfo>().Where(teiFileInfo => teiFileInfo.FileType == 2))
            {
                var memoryStream = new MemoryStream();
                try
                {
                    client.Receive(teiFileInfo.Name, memoryStream);

                    // TODO: Save received message in database
                    
                    // Delete file on FTP
                    client.Delete(teiFileInfo.Name);
                }
                catch (Exception ex)
                {
                    this.LoggerService.Log(this.AccountId, ex);
                    this.LoggerService.Log(this.AccountId, "Error downloading file: " + teiFileInfo.Name, Category.Debug, Priority.High);
                }
            }

            if (client.Active) client.Close();
#27384
Posted: 11/26/2013 01:13:00
by Vsevolod Ievgiienko (EldoS Corp.)

Thank you for contacting us.

Your approach is correct. Thanks for posting code here - it may be useful for other users.

Reply

Statistics

Topic viewed 766 times

Number of guests: 1, registered members: 0, in total hidden: 0




|

Back to top

As of July 15, 2016 EldoS Corporation will operate as a division of /n software inc. For more information, please read the announcement.

Got it!