上传文件使用C#到ftp

编程入门 行业动态 更新时间:2024-10-23 08:27:32
本文介绍了上传文件使用C#到ftp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我试着上传文件到FTP服务器与 C#。该文件上传,但与零字节。

私人无效button2_Click(对象发件人,EventArgs的) {     VAR dirPath = @C:/ Documents和设置/ sander.GD / Bureaublad /测试/;     FTP FtpClient的=新的FTP(ftp://example/,用户名,密码);     字符串[]文件= 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/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/", "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/target.zip", "STOR", localFilePath); }

更多推荐

上传文件使用C#到ftp

本文发布于:2023-11-10 00:56:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1573923.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:上传文件   ftp

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!