上传文件使用C#到ftp [英] Upload file to ftp using c#
本文介绍了上传文件使用C#到ftp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试着上传文件到FTP服务器与 C#
。该文件上传,但与零字节。
私人无效button2_Click(对象发件人,EventArgs的)
{
VAR dirPath = @C:/ Documents和设置/ sander.GD / Bureaublad /测试/;
FTP FtpClient的=新的FTP(ftp://example.com/,用户名,密码);
字符串[]文件= Directory.GetFiles(dirPath,*。*);
VAR uploadPath =/ httpdocs资料/张专辑;
的foreach(在文件中字符串的文件)
{
ftpClient.createDirectory(/测试);
ftpClient.upload(uploadPath +/+ Path.GetFileName(文件),文件);
}
如果(string.IsNullOrEmpty(txtnaam.Text))
{
的MessageBox.show(在德geven Gelieve UW NAAM!);
}
}
解决方案
现有的答案是正确的,但为什么要重新发明轮子和打扰较低水平WebRequest的类型,而 Web客户端
已经实现FTP上传整齐的:
使用(Web客户端的客户端=新的Web客户端())
{
client.Credentials =新的NetworkCredential(ftpUsername,ftpPassword);
client.UploadFile(ftp://ftpserver.com/target.zip,STOR,localFilePath);
}
I try upload a file to an FTP-server with c#
. The file is uploaded but with zero bytes.
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 !");
}
}
解决方案
The existing answers are valid, but why re-invent the wheel and bother with lower level WebRequest types while WebClient
already implements FTP uploading neatly:
using (WebClient client = new WebClient())
{
client.Credentials = new NetworkCredential(ftpUsername, ftpPassword);
client.UploadFile("ftp://ftpserver.com/target.zip", "STOR", localFilePath);
}
这篇关于上传文件使用C#到ftp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文