首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用C#上传文件到FTP

使用C#上传文件到FTP
EN

Stack Overflow用户
提问于 2013-03-07 18:31:14
回答 8查看 299.6K关注 0票数 125

我尝试用C#将文件上传到FTP服务器。文件已上载,但字节为零。

代码语言:javascript
复制
private void button2_Click(object sender, EventArgs e)
{
    var dirPath = @"C:/Documents and Settings/sander.GD/Bureaublad/test/";

    ftp ftpClient = new ftp("ftp://example.com/", "username", "password");

    string[] files = Directory.GetFiles(dirPath,"*.*");

    var uploadPath = "/httpdocs/album";

    foreach (string file in files)
    {
        ftpClient.createDirectory("/test");

        ftpClient.upload(uploadPath + "/" + Path.GetFileName(file), file);
    }

    if (string.IsNullOrEmpty(txtnaam.Text))
    {
        MessageBox.Show("Gelieve uw naam in te geven !");
    }
}
EN

回答 8

Stack Overflow用户

回答已采纳

发布于 2014-10-15 00:23:10

现有的答案是有效的,但当WebClient已经很好地实现了WebRequest上传时,为什么还要重新发明轮子并为较低级别的FTP类型而烦恼:

代码语言:javascript
复制
using (var client = new WebClient())
{
    client.Credentials = new NetworkCredential(ftpUsername, ftpPassword);
    client.UploadFile("ftp://host/path.zip", WebRequestMethods.Ftp.UploadFile, localFile);
}
票数 285
EN

Stack Overflow用户

发布于 2017-08-16 13:42:39

最简单的方法

使用.NET框架将文件上传到FTP服务器的最简单方法是使用WebClient.UploadFile method

代码语言:javascript
复制
WebClient client = new WebClient();
client.Credentials = new NetworkCredential("username", "password");
client.UploadFile(
    "ftp://ftp.example.com/remote/path/file.zip", @"C:\local\path\file.zip");

高级选项

如果您需要更好控制,则使用FtpWebRequest,但WebClient不提供(如TLS/SSL encryption、ascii/文本传输模式、活动模式、传输恢复、进度监控等)。简单的方法是使用Stream.CopyToFileStream复制到FTP流

代码语言:javascript
复制
FtpWebRequest request =
    (FtpWebRequest)WebRequest.Create("ftp://ftp.example.com/remote/path/file.zip");
request.Credentials = new NetworkCredential("username", "password");
request.Method = WebRequestMethods.Ftp.UploadFile;  

using (Stream fileStream = File.OpenRead(@"C:\local\path\file.zip"))
using (Stream ftpStream = request.GetRequestStream())
{
    fileStream.CopyTo(ftpStream);
}

进度监控

如果您需要监控上传进度,您必须自己分块复制内容:

代码语言:javascript
复制
FtpWebRequest request =
    (FtpWebRequest)WebRequest.Create("ftp://ftp.example.com/remote/path/file.zip");
request.Credentials = new NetworkCredential("username", "password");
request.Method = WebRequestMethods.Ftp.UploadFile;  

using (Stream fileStream = File.OpenRead(@"C:\local\path\file.zip"))
using (Stream ftpStream = request.GetRequestStream())
{
    byte[] buffer = new byte[10240];
    int read;
    while ((read = fileStream.Read(buffer, 0, buffer.Length)) > 0)
    {
        ftpStream.Write(buffer, 0, read);
        Console.WriteLine("Uploaded {0} bytes", fileStream.Position);
    } 
}

有关图形用户界面进度(WinForms ProgressBar)的信息,请参阅位于以下位置的C#示例:

How can we show progress bar for upload with FtpWebRequest

上传文件夹

如果要上载文件夹中的所有文件,请参见

Upload directory of files to FTP server using WebClient

有关递归上载的信息,请参见

Recursive upload to FTP server in C#

票数 46
EN

Stack Overflow用户

发布于 2013-03-07 18:37:39

下面的方法对我很有效:

代码语言:javascript
复制
public virtual void Send(string fileName, byte[] file)
{
    ByteArrayToFile(fileName, file);

    var request = (FtpWebRequest) WebRequest.Create(new Uri(ServerUrl + fileName));

    request.Method = WebRequestMethods.Ftp.UploadFile;
    request.UsePassive = false;
    request.Credentials = new NetworkCredential(UserName, Password);
    request.ContentLength = file.Length;

    var requestStream = request.GetRequestStream();
    requestStream.Write(file, 0, file.Length);
    requestStream.Close();

    var response = (FtpWebResponse) request.GetResponse();

    if (response != null)
        response.Close();
}

您不能在您的代码中读取send文件参数,因为它只是文件名。

使用以下内容:

代码语言:javascript
复制
byte[] bytes = File.ReadAllBytes(dir + file);

来获取文件,以便可以将其传递给Send方法。

票数 10
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15268760

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档