C# FTP文件上传下载

1.服务端采用Serv-U FTP Server作为FTP服务器端软件。

2.客户端采用开源库FluentFTP

 

 

 

 

客户端代码

using FluentFTP;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web;

namespace Demo.FTPDownLoad
{
    public static class FTPDownLoadFileHelper
    {
        private static string FtpClientHost = "";//服务端IP
        private static string FtpClientPort = "";//端口号
        private static FluentFTP.FtpClient ftpclient;

        static FTPDownLoadFileHelper()
        {
        
        }
        public static bool FTPDownLoadConnect(string FtpClientHost, int FtpClientPort, string userName, string passKey)
        {
            try
            {
                if (ftpclient != null && ftpclient.IsConnected == true)
                {
                    
                    return true;
                }
                ftpclient = new FluentFTP.FtpClient(FtpClientHost, FtpClientPort, userName, passKey);
                ftpclient.Encoding = Encoding.Default;//设置编码方式

                ftpclient.Connect();//建立连接
                return true;
            }
            catch (Exception ex)
            {
   
                return false;
            }

        }
 
        /// <param name="loaclSaveFilePath">本地保存地址</param>
        /// <param name="ftpPath">远程文件地址</param>
        /// <param name="ProgressAction">下载进度回调</param>
        public static void DownFile(string loaclSaveFilePath, string ftpPath, Action<string, double> ProgressAction)
        {
            try
            {
                if (!ftpclient.IsConnected)
                {
                    ProgressAction?.Invoke(ftpPath, 500);//服务断开连接,触发下载失败回调
                    ftpclient.Dispose();
                    return;
                }
          //单个文件下载完成触发回调
                Progress<double> progress = new Progress<double>(value => {
                    if (value == 100 || value < 0)
                    {
                            ProgressAction?.Invoke(ftpPath, value);
                    }
                });
                if (ftpclient.FileExists(ftpPath))
                {
                    ftpclient.DownloadFile(loaclSaveFilePath, ftpPath, FtpLocalExists.Overwrite, FtpVerify.None, progress);

                }
            }
            catch (Exception ex)
            {
                LoggerHelper.ErrorLog(ex);
            }

        }
        /// <summary>
        /// 文件上传  返回true 上传成功 false 上传失败
        /// 服务器文件若存在自动覆盖
        /// </summary>
        /// <param name="localPath">本地文件地址</param>
        /// <param name="remotePath">远程文件地址</param>
        /// <param name="progressAction">触发事件</param>
        /// <returns></returns>
        public static bool UploadFile(string localPath, string remotePath, Action<string, double> progressAction)
        {
            try
            {
                if (!ftpclient.IsConnected)
                {
                    progressAction?.Invoke(localPath, 500);
                    ftpclient.Dispose();
                    return false;
                }
                Progress<double> progress = new Progress<double>(value => {
                    if (value == 100)
                    {
                        progressAction?.Invoke(localPath, value);
                    }
                });
                if (!File.Exists(localPath)) return false;
                bool isSuccess = ftpclient.UploadFile(localPath, remotePath, FtpExists.Overwrite, true, FtpVerify.Retry, progress);
                return isSuccess;
            }
            catch (Exception ex)
            {
                LoggerHelper.ErrorLog(ex);
                return false;
            }
        }
        public static int UploadFiles(IEnumerable<string> localPaths, string remoteDir)
        {
            int successNum = 0;
            try
            {
                successNum = ftpclient.UploadFiles(localPaths, remoteDir, FtpExists.Overwrite, true, FtpVerify.Retry);
                return successNum;
            }
            catch (Exception ex)
            {
                LoggerHelper.ErrorLog(ex);
                return successNum;
            }
        }
        /// <summary>
        /// 异步上传文件
        /// </summary>
        /// <param name="localPaths"> 本地文件路径列表</param>
        /// <param name="remoteDir"> 远程文件目录</param>
        /// <param name="token">task 设置上传文件夹取消</param>
        /// <returns></returns>
        public static Task<int> UploadFilesAsync(IEnumerable<string> localPaths, string remoteDir, CancellationToken token)
        {
            try
            {
                return ftpclient.UploadFilesAsync(localPaths, remoteDir, FtpExists.Overwrite, true, FtpVerify.Retry, FtpError.None, token);
            }
            catch (Exception)
            {

                throw;
            }
        }
        public static void DisConnect()
        {


        }
    }
}
            

 

 

 

 

 

posted @ 2019-12-10 14:17  无限环  阅读(1860)  评论(0编辑  收藏  举报