-
ftp 客户端使用案例作者 : Jacky 发布于 2023-05-29 11:30:11 浏览 636 次
使用之前,请记得nuget using XiaoFeng.Ftp包
使用代码如下:
var ftClient= new FtpClient(new FtpClientConfig() { Host="192.168.1.66", Port=21, UserName="admin",Password="111111" }); //切换ftp服务器的目录 var lll= await ftClient.ChangeDirectoryAsync("img"); //本地文件上传到ftp var ll= await ftClient.NewPutByGuid("C:\\Users\\user\\Desktop\\photo\\", "2.jpg"); //从ftp下载文件到本地 var l1l = ftClient.DownFileAsync("\\img\\1.png", "C:\\Users\\user\\Desktop\\photo\\","test.jpg");
如果想使用接口依赖注入的方式,则这样使用:
using XiaoFeng.Ftp; using XiaoFeng.IO; var builder = WebApplication.CreateBuilder(args); builder.Services.AddScoped<IFtpClient>((serviceProvider) => new FtpClient(new FtpClientConfig() { Host = "192.168.1.66", Port = 21, UserName = "admin", Password = "111111" }));
然后在你的服务内使用构造函数进行注入:
/// <summary> /// /// </summary> public readonly IFtpClient _ftpClient; /// <summary> /// /// </summary> /// <param name="ftpClient"></param> public ManageController(IFtpClient ftpClient) { _ftpClient = ftpClient; } /// <summary> /// 测试ftp /// </summary> /// <returns></returns> [HttpGet] public async Task<bool> PushTemperatureAlarm() { var klkl = await _ftpClient.DownFileAsync("\\img\\1.png", "C:\\Users\\user\\Desktop\\photo\\", "11111.jpg"); await Task.CompletedTask; return true; }
上面是使用的大致demo
下面我们列举,连接配置字段:/// <summary> /// 帐号 /// </summary> [Description("帐号")] public string UserName { get; set; } /// <summary> /// 密码 /// </summary> [Description("密码")] public string Password { get; set; } /// <summary> /// 主机 /// </summary> [Description("主机")] public string Host { get; set; } /// <summary> /// 端口 /// </summary> [Description("端口")] public int Port { get; set; } = 21; /// <summary> /// 连接超时 /// </summary> [Description("连接超时时长,单位是毫秒")] public int Timeout { get; set; } = 100_000; /// <summary> /// 读写超时时长,单位是毫秒 /// </summary> [Description("读写超时时长,单位是毫秒")] public int ReadWriteTimeout { get; set; } = 300_000; /// <summary> /// 断开超时时长 /// </summary> [Description("断开超时时长,单位是毫秒")] public int DisconnectTimeout { get; set; } = 300_000; /// <summary> /// 接收超时时长 /// </summary> [Description("接收超时时长,单位是毫秒")] public int ReceiveTimeout { get; set; } = 300; /// <summary> /// 发送超时时长 /// </summary> [Description("发送超时时长,单位是毫秒")] public int SendTimeout { get; set; } = 300; /// <summary> /// 重试连接次数 /// </summary> [Description("重试连接次数")] public int ConnectCount { get; set; } /// <summary> /// 本地目录 /// </summary> [Description("本地目录")] public string LocalDirectory { get; set; } /// <summary> /// 服务器目录 /// </summary> public string RemoteDirectory { get; set; } = "/"; /// <summary> /// 编码名称 /// </summary> [Description("编码名称")] public string CharsetName { get; set; } /// <summary> /// 代理地址 /// </summary> [Description("代理地址")] public string ProxyHost { get; set; } /// <summary> /// 代理端口 /// </summary> [Description("代理端口")] public int ProxyPort { get; set; } /// <summary> /// 代理帐号 /// </summary> [Description("代理帐号")] public string ProxyUserName { get; set; } /// <summary> /// 代理密码 /// </summary> [Description("代理密码")] public string ProxyPassword { get; set; } /// <summary> /// 是否使用SSL链接 /// </summary> [Description("是否使用SSL链接")] public Boolean EnableSSL { get; set; } = false; /// <summary> /// 是否允许二进制 /// </summary> [Description("是否允许二进制")] public Boolean UseBinary { get; set; } = true; /// <summary> /// 是否允许被动式 /// </summary> [Description("是否允许被动式")] public Boolean UsePassive { get; set; } = true; /// <summary> /// 是否请求完成关闭链接 /// </summary> [Description("是否请求完成保持链接")] public Boolean KeepAlive { get; set; } = true;
所有评论(0)