gpt4 book ai didi

c# - 使用C#上传文件到FTP

转载 作者:IT王子 更新时间:2023-10-29 03:36:00 24 4
gpt4 key购买 nike

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

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 !");
}
}

最佳答案

现有的答案是有效的,但为什么要重新发明轮子并为较低的水平而烦恼WebRequest键入 WebClient已经巧妙地实现了 FTP 上传:

using (var client = new WebClient())
{
client.Credentials = new NetworkCredential(ftpUsername, ftpPassword);
client.UploadFile("ftp://host/path.zip", WebRequestMethods.Ftp.UploadFile, localFile);
}

关于c# - 使用C#上传文件到FTP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15268760/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com