using Renci.SshNet; /// <summary> /// 上传文件到ftp服务器上 /// </summary> /// <param name="file">文件</param> /// <param name="dateStr">文件时间戳</param> /// <returns>bool</returns> public static bool UploadFile(IFormFile file, long dateStr) { bool result = false; using (var client = new SftpClient(ftpAddress, 22, ftpUser, ftpPwd)) //创建连接对象,ftpAddress是ip地址如: 47.100.11.12 第二个参数是端口号,第三四个是用户名密码 { client.Connect(); //连接 MemoryStream fs = new MemoryStream(); file.CopyTo(fs); client.BufferSize = 4 * 1024 * 1024; fs.Seek(0, SeekOrigin.Begin); client.UploadFile(fs, "/DirkWang/" + dateStr.ToString() + "_" + file.FileName); fs.Dispose(); result = true; } return result; }
/// <summary> /// 下载附件 /// </summary> /// <param name="fileName">附件名</param> /// <returns>byte[]</returns> public static byte[] DownloadFile(string fileName) { byte[] buffer = new byte[2 * 1024 * 1024]; using (var client = new SftpClient(ftpAddress, 22, ftpUser, ftpPwd)) //创建连接对象 { client.Connect(); //连接 buffer = client.ReadAllBytes("/" + fileName); } return buffer; }