FluentFTP简单使用

流利的FTP 

github地址:https://github.com/robinrodricks/FluentFTP

一、使用原因

  对于ftp,自己本身是不熟悉的。第一次听说这个名词还是在嘉兴项目上,有个老哥要做FTP的上传,但是当时这块功能不是我做的。后来在当前公司,由于业务的扩大,mes中的图片自动上传经常卡顿,也是研究了一下mes中的ftp上传的代码。后来为了保证现场生产的节拍,修改了对图片展示的代码,但是对于ftp未上传成功的数据要校验(此图片正常要求保存30年,因此如果ftp服务器中图片丢失,要手动从ai原图拉去图片),并补充图片。

二、基本使用方法

// create an FTP client
FtpClient client = new FtpClient("123.123.123.123");

// specify the login credentials, unless you want to use the "anonymous" user account
client.Credentials = new NetworkCredential("david", "pass123");

// begin connecting to the server
client.Connect();

// get a list of files and directories in the "/htdocs" folder
foreach (FtpListItem item in client.GetListing("/htdocs")) {
    
    // if this is a file
    if (item.Type == FtpFileSystemObjectType.File){
        
        // get the file size
        long size = client.GetFileSize(item.FullName);
        
        // calculate a hash for the file on the server side (default algorithm)
        FtpHash hash = client.GetChecksum(item.FullName);
    }
    
    // get modified date/time of the file or folder
    DateTime time = client.GetModifiedTime(item.FullName);
    
}

// upload a file
client.UploadFile(@"C:\MyVideo.mp4", "/htdocs/MyVideo.mp4");

// rename the uploaded file
client.Rename("/htdocs/MyVideo.mp4", "/htdocs/MyVideo_2.mp4");

// download the file again
client.DownloadFile(@"C:\MyVideo_2.mp4", "/htdocs/MyVideo_2.mp4");

// compare the downloaded file with the server
if (client.CompareFile(@"C:\MyVideo_2.mp4", "/htdocs/MyVideo_2.mp4") == FtpCompareResult.Equal){  }

// delete the file
client.DeleteFile("/htdocs/MyVideo_2.mp4");

// upload a folder and all its files
client.UploadDirectory(@"C:\website\videos\", @"/public_html/videos", FtpFolderSyncMode.Update);

// upload a folder and all its files, and delete extra files on the server
client.UploadDirectory(@"C:\website\assets\", @"/public_html/assets", FtpFolderSyncMode.Mirror);

// download a folder and all its files
client.DownloadDirectory(@"C:\website\logs\", @"/public_html/logs", FtpFolderSyncMode.Update);

// download a folder and all its files, and delete extra files on disk
client.DownloadDirectory(@"C:\website\dailybackup\", @"/public_html/", FtpFolderSyncMode.Mirror);

// delete a folder recursively
client.DeleteDirectory("/htdocs/extras/");

// check if a file exists
if (client.FileExists("/htdocs/big2.txt")){ }

// check if a folder exists
if (client.DirectoryExists("/htdocs/extras/")){ }

// upload a file and retry 3 times before giving up
client.RetryAttempts = 3;
client.UploadFile(@"C:\MyVideo.mp4", "/htdocs/big.txt", FtpRemoteExists.Overwrite, false, FtpVerify.Retry);

// disconnect! good bye!
client.Disconnect();

 

 

三、项目中简单使用

  

//FTP client
FtpClient client;
//客户端初始化
client = new FtpClient(new Uri("ftp://mesdata"), "123", "123");
client.Connect();
//目标文件夹格式 string fomatFtp = "yyyy年/M月/yyyy-M-d"; #region 使用AI原图补充图片 //FTP地址补充 DateTime dtime = Convert.ToDateTime(data.RECORDDATE); ftpPath = ftpPath + dtime.ToString(fomatFtp) + "/" + data.LOTNUMBER + ".jpg"; FtpStatus f = client.UploadFile(localPath, ftpPath); if (f == FtpStatus.Success) {   //更新状态 2:校验成功   UpdateStatusInfo(data, "2");   //删除本地图片   File.Delete(localPath); } else {   //更新状态 3:校验失败   UpdateStatusInfo(data, "3"); } #endregion

 

posted @ 2020-09-06 09:56  hangxing.pang  阅读(8032)  评论(0编辑  收藏  举报