FtpUpload.cs
1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using FluentFTP;
using Microsoft.AspNetCore.Http;
using System.IO;
using System.Net;
namespace UEditorNetCore
{
public class FtpUpload
{
/// <summary>
/// 文件上传
/// </summary>
/// <param name="file">上传文件</param>
/// <param name="saveFilePath">保存完整路径/param>
/// <param name="ip">保存服务器IP</param>
/// <param name="username">用户名</param>
/// <param name="pwd">密码</param>
/// <returns></returns>
public static bool UploadFile(IFormFile file, string saveFilePath, string ip, string username, string pwd)
{
var result = UploadFile(file.OpenReadStream(), saveFilePath, ip, username, pwd);
return result;
}
/// <summary>
/// 文件上传
/// </summary>
/// <param name="fileStream">文件流</param>
/// <param name="saveFilePath">保存完整路径</param>
/// <param name="ip">保存服务器IP</param>
/// <param name="username">用户名</param>
/// <param name="pwd">密码</param>
/// <returns></returns>
public static bool UploadFile(Stream fileStream, string saveFilePath, string ip, string username, string pwd)
{
var result = false;
var client = new FtpClient(ip)
{
Credentials = new NetworkCredential(username, pwd)
};
try
{
client.Connect();
client.Upload(fileStream, saveFilePath, createRemoteDir: true);
if (client.FileExists(saveFilePath))
{
result = true;
}
client.Disconnect();
}
catch (FtpException ex)
{
client.Disconnect();
//
}
return result;
}
}
}