/// <summary> /// 从FTP服务器下载文件,指定本地路径和本地文件名 /// </summary> /// <param name="remoteFileName">远程文件名</param> /// <param name="localFileName">保存本地的文件名(包含路径)</param> /// <param name="ifCredential">是否启用身份验证(false:表示允许用户匿名下载)</param> /// <param name="updateProgress">报告进度的处理(第一个参数:总大小,第二个参数:当前进度)</param> public void Download(string remoteFileName, string localFileName, bool ifCredential = false, Action <int, int> updateProgress = null) { using (FileStream outputStream = new FileStream(localFileName, FileMode.Create)) { if (FtpServer == null || FtpServer.Trim().Length == 0) { throw new Exception("ftp下载目标服务器地址未设置!"); } Uri uri = new Uri("ftp://" + FtpServer + "/" + remoteFileName); var ftpsize = (FtpWebRequest)WebRequest.Create(uri); ftpsize.UseBinary = true; var reqFtp = (FtpWebRequest)WebRequest.Create(uri); reqFtp.UseBinary = true; reqFtp.KeepAlive = false; if (ifCredential) //使用用户身份认证 { ftpsize.Credentials = new NetworkCredential(Username, Password); reqFtp.Credentials = new NetworkCredential(Username, Password); } ftpsize.Method = WebRequestMethods.Ftp.GetFileSize; using (FtpWebResponse re = (FtpWebResponse)ftpsize.GetResponse()) { long totalBytes = re.ContentLength; reqFtp.Method = WebRequestMethods.Ftp.DownloadFile; var response = (FtpWebResponse)reqFtp.GetResponse(); using (response) { var ftpStream = response.GetResponseStream(); //更新进度 using (ftpStream) { updateProgress?.Invoke((int)totalBytes, 0); //更新进度条 long totalDownloadedByte = 0; int bufferSize = 1024 * 1024; byte[] buffer = new byte[bufferSize]; if (ftpStream != null) { var readCount = ftpStream.Read(buffer, 0, bufferSize); while (readCount > 0) { totalDownloadedByte = readCount + totalDownloadedByte; outputStream.Write(buffer, 0, readCount); //更新进度 updateProgress?.Invoke((int)totalBytes, (int)totalDownloadedByte); //更新进度条 readCount = ftpStream.Read(buffer, 0, bufferSize); } } } } } } }
/// <summary> /// 上传文件到FTP服务器 /// </summary> /// <param name="relativePath">相对目录</param> /// <param name="localFullPathName">本地带有完整路径的文件名</param> /// <param name="updateProgress">报告进度的处理(第一个参数:总大小,第二个参数:当前进度)</param> public void UploadFile(string relativePath, string localFullPathName, Action <int, int> updateProgress = null) { FileInfo finfo = new FileInfo(localFullPathName); if (FtpServer == null || FtpServer.Trim().Length == 0) { throw new Exception("ftp上传目标服务器地址未设置!"); } Uri uri = new Uri("ftp://" + FtpServer + "/" + relativePath + "/" + finfo.Name); var reqFtp = (FtpWebRequest)WebRequest.Create(uri); reqFtp.KeepAlive = false; reqFtp.UseBinary = true; reqFtp.Credentials = new NetworkCredential(Username, Password); //用户,密码 reqFtp.Method = WebRequestMethods.Ftp.UploadFile; //向服务器发出下载请求命令 reqFtp.ContentLength = finfo.Length; //为request指定上传文件的大小 int buffLength = 1024 * 1024; byte[] buff = new byte[buffLength]; using (var fs = finfo.OpenRead()) { using (var stream = reqFtp.GetRequestStream()) { var contentLen = fs.Read(buff, 0, buffLength); int allbye = (int)finfo.Length; //更新进度 updateProgress?.Invoke(allbye, 0); //更新进度条 int startbye = 0; while (contentLen != 0) { startbye = contentLen + startbye; stream.Write(buff, 0, contentLen); //更新进度 updateProgress?.Invoke(allbye, startbye); //更新进度条 contentLen = fs.Read(buff, 0, buffLength); } } } }
/// <summary> /// 上传文件到FTP服务器 /// </summary> /// <param name="relativePath">相对目录</param> /// <param name="localFullPathName">本地带有完整路径的文件名</param> /// <param name="updateProgress">报告进度的处理(第一个参数:总大小,第二个参数:当前进度)</param> public void UploadFile(string relativePath, string localFullPathName, Action <int, int> updateProgress = null) { var finfo = new FileInfo(localFullPathName); if (FtpServer == null || FtpServer.Trim().Length == 0) { throw new Exception("ftp上传目标服务器地址未设置!"); } var reqFtp = GenerateFtpReq(StringUtils.GenerateUri(new string[] { FtpServer, relativePath, finfo.Name }, "ftp://"), WebRequestMethods.Ftp.UploadFile); reqFtp.UseBinary = true; reqFtp.ContentLength = finfo.Length; //为request指定上传文件的大小 int buffLength = 1024 * 1024; byte[] buff = new byte[buffLength]; using (var fs = finfo.OpenRead()) { using (var stream = reqFtp.GetRequestStream()) { var contentLen = fs.Read(buff, 0, buffLength); int allbye = (int)finfo.Length; //更新进度 updateProgress?.Invoke(allbye, 0); //更新进度条 int startbye = 0; while (contentLen != 0) { startbye = contentLen + startbye; stream.Write(buff, 0, contentLen); //更新进度 updateProgress?.Invoke(allbye, startbye); //更新进度条 contentLen = fs.Read(buff, 0, buffLength); } } } }