当前位置:首页 > 行业动态 > 正文

c#将本地文件上传到ftp服务器端

摘要:本文介绍了使用C#语言将本地文件上传至FTP服务器端的方法,包括设置连接参数、创建连接、上传文件等步骤。

在C#中,将本地文件上传到FTP服务器端可以通过多种方式实现,以下是几种常见的方法:

c#将本地文件上传到ftp服务器端  第1张

使用FtpWebRequest类

1、创建请求并设置相关属性:首先需要创建一个FtpWebRequest对象,并设置其相关属性,如服务器地址、用户名、密码、上传文件的方法等。

string ftpServer = "ftp://yourserver.com/";
string username = "yourusername";
string password = "yourpassword";
string filePath = @"C:pathtolocalfile.txt";
string uploadFileName = "file.txt";
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri(ftpServer + uploadFileName));
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential(username, password);
request.UseBinary = true;
request.KeepAlive = false;

2、读取本地文件并写入请求流:通过FileStream读取本地文件的内容,并将其写入到FtpWebRequest的请求流中。

using (FileStream stream = new FileStream(filePath, FileMode.Open))
{
    byte[] buffer = new byte[stream.Length];
    stream.Read(buffer, 0, buffer.Length);
    using (Stream requestStream = request.GetRequestStream())
    {
        requestStream.Write(buffer, 0, buffer.Length);
    }
}

3、获取响应并处理结果:获取FtpWebRequest的响应,并根据需要进行相应的处理,如检查是否上传成功等。

FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Console.WriteLine($"Upload File Complete, Status={response.StatusDescription}");
response.Close();

使用第三方库(如Flurl.Http)

1、安装Flurl.Http库:首先需要在项目中安装Flurl.Http库,可以使用NuGet包管理器进行安装。

2、编写上传代码:使用Flurl.Http提供的简洁API来编写上传文件的代码。

string ftpUrl = "ftp://yourserver.com/";
string username = "yourusername";
string password = "yourpassword";
string localFilePath = @"C:pathtolocalfile.txt";
string remoteFileName = "file.txt";
var client = new FlurlClient(ftpUrl)
    .WithBasicAuth(username, password)
    .UploadFileMultipartAsync(remoteFileName, localFilePath)
    .Result;
if (client.IsSuccessStatusCode)
{
    Console.WriteLine("File uploaded successfully!");
}
else
{
    Console.WriteLine("Failed to upload file.");
}

使用第三方库(如SSH.NET)

1、安装SSH.NET库:同样需要在项目中安装SSH.NET库。

2、建立连接并上传文件:使用SSH.NET库建立与FTP服务器的连接,并执行上传文件的操作。

using Renci.SshNet;
using System;
using System.IO;
class Program
{
    static void Main()
    {
        string host = "yourserver.com";
        int port = 21; // 默认FTP端口为21
        string username = "yourusername";
        string password = "yourpassword";
        string localFilePath = @"C:pathtolocalfile.txt";
        string remoteFilePath = "/remote/path/to/file.txt";
        using (var client = new SftpClient(host, port, username, password))
        {
            try
            {
                client.Connect();
                using (var localStream = new FileStream(localFilePath, FileMode.Open))
                {
                    client.UploadFile(localStream, remoteFilePath);
                }
                Console.WriteLine("File uploaded successfully!");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: " + ex.Message);
            }
            finally
            {
                client.Disconnect();
            }
        }
    }
}

三种方法都可以实现将本地文件上传到FTP服务器端的功能,开发者可以根据自己的需求和项目的实际情况选择合适的方法。

0