Blog

Filter posts by Category Or Tag of the Blog section!

Using FluentFTP in Asp.net applications

Saturday, 27 February 2016

FluentFTP is a lightweight opensource library for FTP operations and I have used it several times in the applications I have worked on. You can refer to the documentation of the project on GitHub for more information. But I'm just gonna share with you the distilled helper I'm using for FluentFTP.

 


 public class FtpManager

    {

        private readonly string _ftpUrl;

        private readonly string _username;

        private readonly string _password;

 

        public FtpManager(string ftpUrl, string username, string password)

        {

            if (string.IsNullOrEmpty(ftpUrl))

            {

                throw new ArgumentNullException(nameof(ftpUrl));

            }

 

            if (string.IsNullOrEmpty(username))

            {

                throw new ArgumentNullException(nameof(username));

            }

 

            if (string.IsNullOrEmpty(password))

            {

                throw new ArgumentNullException(nameof(password));

            }

 

 

            if (!ftpUrl.StartsWith("ftp://"))

            {

                ftpUrl = $"ftp://{ftpUrl}";

            }

 

            _ftpUrl = ftpUrl;

            _username = username;

            _password = password;

        }

 

        public void UploadFile(AttachmentFileModel attachmentFileModel)

        {

            try

            {

                var client = new FtpClient(_ftpUrl);

 

 

                byte[] data;

                using (var inputStream = attachmentFileModel.HttpPostedFileBase.InputStream)

                {

                    var memoryStream = inputStream as MemoryStream;

                    if (memoryStream == null)

                    {

                        memoryStream = new MemoryStream();

                        inputStream.CopyTo(memoryStream);

                    }

 

                    data = memoryStream.ToArray();

                }

 

                client.Credentials = new NetworkCredential(_username, _password);

                client.Connect();

                var path = CreateDirectory(attachmentFileModel, client);

                client.Upload(data, path + attachmentFileModel.ContentName);

                client.Disconnect();

            }

            catch (Exception exception)

            {

                //Log

            }

        }

 

        public void UpdateFile(AttachmentFileModel attachmentFileModel)

        {

            try

            {

                var client = new FtpClient(_ftpUrl);

 

                byte[] data;

                using (var inputStream = attachmentFileModel.HttpPostedFileBase.InputStream)

                {

                    var memoryStream = inputStream as MemoryStream;

                    if (memoryStream == null)

                    {

                        memoryStream = new MemoryStream();

                        inputStream.CopyTo(memoryStream);

                    }

 

                    data = memoryStream.ToArray();

                }

 

                client.Credentials = new NetworkCredential(_username, _password);

                client.Connect();

                var path = CreateDirectory(attachmentFileModel, client);

                client.Upload(data, path + attachmentFileModel.ContentName);

                client.Disconnect();

            }

            catch (Exception exception)

            {

                //Log it

            }

        }

 

        public void DeleteFile(AttachmentFileModel attachmentFileModel)

        {

            try

            {

                var client = new FtpClient(_ftpUrl);

                client.Credentials = new NetworkCredential(_username, _password);

                client.Connect();

                var path = CreateDirectory(attachmentFileModel, client);

 

                if (client.FileExists(path + attachmentFileModel.ContentName))

                {

                    client.DeleteFile(path + attachmentFileModel.ContentName);

                }

 

                client.Disconnect();

            }

            catch (Exception exception)

            {

                //Log

            }

        }

 

        private static string CreateDirectory(AttachmentFileModel attachmentFileModel, FtpClient client)

        {

            string extension;

            if (attachmentFileModel.HttpPostedFileBase != null)

            {

                extension = Path.GetExtension(attachmentFileModel.HttpPostedFileBase.FileName);

            }

            else

            {

                extension = attachmentFileModel.FileName;

            }

 

            extension = extension.Replace(".", "");

            var path = string.Format("FolderName-" + extension + "/");

            if (!client.DirectoryExists(path))

                client.CreateDirectory(path);

            return path;

        }

    }

 

As I insinuated, this is a distilled helper and basic usage of CRUD on FTP, you can change it based on your needs as I have done many times. Cheers!

Category: Software

Tags: Libraries

comments powered by Disqus