侧边栏壁纸
博主头像
分享你我博主等级

行动起来,活在当下

  • 累计撰写 107 篇文章
  • 累计创建 13 个标签
  • 累计收到 0 条评论

目 录CONTENT

文章目录

.net core 使用FluentFTP 实现FTP上传、下载文件

管理员
2020-09-24 / 0 评论 / 0 点赞 / 4 阅读 / 4429 字

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

FluentFTP 支持上传下载断点续传

在工具台引入

Install-Package FluentFTP -Version 32.4.5

然后使用

// 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
await client.ConnectAsync();
// get a list of files and directories in the "/htdocs" folder
foreach (FtpListItem item in await client.GetListingAsync("/htdocs")) {
// if this is a file
if (item.Type == FtpFileSystemObjectType.File){
// get the file size
long size = await client.GetFileSizeAsync(item.FullName);
// calculate a hash for the file on the server side (default algorithm)
FtpHash hash = await client.GetChecksumAsync(item.FullName);
}
// get modified date/time of the file or folder
DateTime time = await client.GetModifiedTimeAsync(item.FullName);
}
// upload a file
await client.UploadFileAsync(@"C:\MyVideo.mp4", "/htdocs/MyVideo.mp4");
// rename the uploaded file
await client.RenameAsync("/htdocs/MyVideo.mp4", "/htdocs/MyVideo_2.mp4");
// download the file again
await client.DownloadFileAsync(@"C:\MyVideo_2.mp4", "/htdocs/MyVideo_2.mp4");
// compare the downloaded file with the server
if (await client.CompareFileAsync(@"C:\MyVideo_2.mp4", "/htdocs/MyVideo_2.mp4") == FtpCompareResult.Equal){  }
// delete the file
await client.DeleteFileAsync("/htdocs/MyVideo_2.mp4");
// upload a folder and all its files
await client.UploadDirectoryAsync(@"C:\website\videos\", @"/public_html/videos", FtpFolderSyncMode.Update);
// upload a folder and all its files, and delete extra files on the server
await client.UploadDirectoryAsync(@"C:\website\assets\", @"/public_html/assets", FtpFolderSyncMode.Mirror);
// download a folder and all its files
await client.DownloadDirectoryAsync(@"C:\website\logs\", @"/public_html/logs", FtpFolderSyncMode.Update);
// download a folder and all its files, and delete extra files on disk
await client.DownloadDirectoryAsync(@"C:\website\dailybackup\", @"/public_html/", FtpFolderSyncMode.Mirror);
// delete a folder recursively
await client.DeleteDirectoryAsync("/htdocs/extras/");
// check if a file exists
if (await client.FileExistsAsync("/htdocs/big2.txt")){ }
// check if a folder exists
if (await client.DirectoryExistsAsync("/htdocs/extras/")){ }
// upload a file and retry 3 times before giving up
client.RetryAttempts = 3;
await client.UploadFileAsync(@"C:\MyVideo.mp4", "/htdocs/big.txt", FtpRemoteExists.Overwrite, false, FtpVerify.Retry);
// disconnect! good bye!
await client.DisconnectAsync();

相关帮助文档:https://github.com/robinrodricks/FluentFTP/wiki/Quick-Start-Example

0

评论区