Exemple #1
0
        /// <summary>
        /// 上传
        /// </summary>
        /// <param name="localfile"></param>
        /// <param name="remotefile"></param>
        /// <param name="mode">模式</param>
        /// <returns></returns>
        public Int64 UploadFile(String localfile, String remotefile, FtpTransportMode mode)
        {
            if (String.IsNullOrEmpty(localfile))
            {
                throw new ArgumentNullException("localfile", "没有指定本地文件名。");
            }

            var fi = new FileInfo(localfile);

            if (!fi.Exists)
            {
                throw new Exception("本地文件不存在。");
            }

            if (String.IsNullOrEmpty(remotefile))
            {
                remotefile = fi.Name;
            }

            var size = Client.UploadFile(localfile, FtpPath.Combine(Path, remotefile), mode);

            Reload();

            return(size);
        }
Exemple #2
0
        /// <summary>
        /// 下载目录
        /// </summary>
        /// <param name="dir"></param>
        /// <param name="localpath"></param>
        /// <param name="mode"></param>
        /// <returns></returns>
        public Int64 DownloadDirectory(FtpDirectory dir, String localpath, FtpTransportMode mode)
        {
            if (dir == null)
            {
                throw new ArgumentNullException("dir");
            }
            if (dir.Count < 1)
            {
                return(0);
            }
            if (String.IsNullOrEmpty(localpath))
            {
                throw new ArgumentNullException("localpath");
            }

            if (localpath[1] != Path.VolumeSeparatorChar)
            {
                localpath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, localpath);
            }

            Int64 count = 0;

            //遍历
            foreach (var item in dir)
            {
                if (item.IsDirectory)
                {
                    //递归
                    var d = ListDirectoryDetails(item.FullName);
                    count += DownloadDirectory(d, Path.Combine(localpath, item.FileName), mode);
                }
                else
                {
                    //下载文件
                    count += DownloadFile(item.FullName, Path.Combine(localpath, item.FileName), mode);
                }
            }
            return(count);
        }
Exemple #3
0
        /// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="remotefile">远程文件</param>
        /// <param name="localfile">本地文件</param>
        /// <param name="mode">端点续传模式</param>
        /// <returns></returns>
        public Int64 DownloadFile(String remotefile, String localfile, FtpTransportMode mode)
        {
            #region 准备工作
            if (String.IsNullOrEmpty(remotefile))
            {
                return(0);
            }
            if (String.IsNullOrEmpty(localfile))
            {
                var str = remotefile;
                if (str.Contains("/"))
                {
                    str = str.Substring(str.LastIndexOf("/") + 1);
                }
                localfile = str;
            }
            if (localfile[1] != Path.VolumeSeparatorChar)
            {
                localfile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, localfile);
            }

            if (File.Exists(localfile))
            {
                if (mode == FtpTransportMode.OverWrite)
                {
                    File.Delete(localfile);
                }
                else if (mode == FtpTransportMode.None)
                {
                    return(0);
                }
                //throw new Exception("目标文件已存在!");
            }
            #endregion

            var e = new LoadFileEventArgs();
            if (OnDownloadFile != null)
            {
                e.Cancel = false;
                e.Src    = remotefile;
                e.Des    = localfile;
                var fi = new FileInfo(localfile);
                if (fi != null && fi.Exists)
                {
                    e.DesSize = fi.Length;
                }

                OnDownloadFile(this, e);

                if (e.Cancel)
                {
                    return(0);
                }
            }

            try
            {
                #region  载
                var ftp = GetFtpRequest(remotefile);
                ftp.Method    = WebRequestMethods.Ftp.DownloadFile;
                ftp.UseBinary = true;

                if (!Directory.Exists(Path.GetDirectoryName(localfile)))
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(localfile));
                }
                var fs = new FileStream(localfile, FileMode.OpenOrCreate, FileAccess.Write);
                //如果采用断点续传,则从断点处开始
                if (mode == FtpTransportMode.Append)
                {
                    //取远程文件大小
                    var size = GetFileSize(remotefile);
                    if (fs.Length >= size)
                    {
                        return(0);
                    }

                    //设置断点
                    ftp.ContentOffset = fs.Length;
                }

                var   buffer = new Byte[2048];
                Int64 count  = 0;
                using (var response = GetResponse(ftp))
                {
                    using (var responseStream = response.GetResponseStream())
                    {
                        try
                        {
                            var read = 0;
                            do
                            {
                                read   = responseStream.Read(buffer, 0, buffer.Length);
                                count += read;
                                fs.Write(buffer, 0, read);
                            } while (read != 0);
                        }
                        finally
                        {
                            fs.Close();
                            responseStream.Close();
                        }
                    }

                    response.Close();
                }
                e.SrcSize = count;
                return(count);

                #endregion
            }
            finally
            {
                if (OnDownloadFileFinished != null)
                {
                    var fi = new FileInfo(localfile);
                    if (fi != null && fi.Exists)
                    {
                        e.DesSize = fi.Length;
                    }

                    OnDownloadFileFinished(this, e);
                }
            }
        }
Exemple #4
0
        /// <summary>
        /// 上传目录
        /// </summary>
        /// <param name="localpath"></param>
        /// <param name="dir"></param>
        /// <param name="mode"></param>
        /// <returns></returns>
        public Int64 UploadDirectory(String localpath, FtpDirectory dir, FtpTransportMode mode)
        {
            if (String.IsNullOrEmpty(localpath))
            {
                throw new ArgumentNullException("localpath");
            }
            if (dir == null)
            {
                throw new ArgumentNullException("dir");
            }

            if (localpath[1] != Path.VolumeSeparatorChar)
            {
                localpath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, localpath);
            }

            var   di   = new DirectoryInfo(localpath);
            Int64 size = 0;

            //遍历文件
            var files = di.GetFiles();

            if (files != null && files.Length > 0)
            {
                foreach (var item in files)
                {
                    //检查远端文件是否存在
                    if (dir.Contains(item.Name))
                    {
                        if (mode == FtpTransportMode.None)
                        {
                            continue;
                        }

                        //删除
                        if (mode == FtpTransportMode.OverWrite)
                        {
                            dir.Files[item.Name].Delete();
                        }
                    }

                    //上传文件
                    //UploadFile(item.FullName, f.FullName);
                    size += dir.UploadFile(item.FullName, mode);
                }
            }

            //遍历目录
            var dis = di.GetDirectories();

            if (dis != null && dis.Length > 0)
            {
                foreach (var item in dis)
                {
                    //检查远端文件夹是否存在
                    if (!dir.Contains(item.Name))
                    {
                        dir.CreateDirectory(item.Name);
                    }

                    //递归
                    size += UploadDirectory(item.FullName, dir.Directories[item.Name], mode);
                }
            }
            return(size);
        }
Exemple #5
0
        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="localfile">本地文件</param>
        /// <param name="remotefile">远程文件</param>
        /// <param name="mode">模式</param>
        /// <returns></returns>
        public Int64 UploadFile(String localfile, String remotefile, FtpTransportMode mode)
        {
            #region 准备工作
            if (String.IsNullOrEmpty(localfile))
            {
                throw new ArgumentNullException("localfile", "没有指定本地文件名。");
            }
            if (String.IsNullOrEmpty(remotefile))
            {
                throw new ArgumentNullException("remotefile", "没有指定远程文件名。");
            }

            if (localfile[1] != Path.VolumeSeparatorChar)
            {
                localfile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, localfile);
            }

            var fi = new FileInfo(localfile);
            if (!fi.Exists)
            {
                throw new Exception("本地文件不存在。");
            }

            //if (String.IsNullOrEmpty(remotefile)) remotefile = fi.Name;

            var e = new LoadFileEventArgs();
            if (OnUploadFile != null)
            {
                e.Cancel  = false;
                e.Src     = localfile;
                e.SrcSize = fi.Length;
                e.Des     = remotefile;

                OnUploadFile(this, e);

                if (e.Cancel)
                {
                    return(0);
                }
            }
            #endregion

            try
            {
                #region
                var ftp = GetFtpRequest(remotefile);
                ftp.Method    = WebRequestMethods.Ftp.UploadFile;
                ftp.UseBinary = true;

                var   buffer = new Byte[2048];
                Int64 size   = 0;
                using (var fs = fi.OpenRead())
                {
                    try
                    {
                        //如果采用断点续传,则从断点处开始
                        if (mode == FtpTransportMode.Append)
                        {
                            //取远程文件大小
                            var size2 = GetFileSize(remotefile);
                            if (fs.Length <= size2)
                            {
                                return(size2);
                            }

                            //设置断点
                            ftp.ContentOffset = size2;
                            fs.Position       = size2;
                        }

                        using (var rs = ftp.GetRequestStream())
                        {
                            Int32 dataRead;
                            do
                            {
                                dataRead = fs.Read(buffer, 0, buffer.Length);
                                size    += dataRead;
                                rs.Write(buffer, 0, dataRead);
                            } while (dataRead >= buffer.Length);
                            rs.Close();
                        }
                    }
                    finally
                    {
                        fs.Close();
                    }
                }
                e.DesSize = size;
                return(size);

                #endregion
            }
            finally
            {
                OnUploadFileFinished?.Invoke(this, e);
            }
        }
Exemple #6
0
 /// <summary>
 /// 下载该文件
 /// </summary>
 /// <param name="localfile"></param>
 /// <param name="mode"></param>
 public void Download(String localfile, FtpTransportMode mode)
 {
     Client.DownloadFile(FullName, localfile, mode);
 }
Exemple #7
0
 /// <summary>
 /// 上传
 /// </summary>
 /// <param name="localfile"></param>
 /// <param name="mode">模式</param>
 /// <returns></returns>
 public Int64 UploadFile(String localfile, FtpTransportMode mode) => UploadFile(localfile, null, mode);