我尝试使用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 !");
}
}
Run Code Online (Sandbox Code Playgroud) 以下代码旨在通过FTP检索文件.但是,我收到了一个错误.
serverPath = "ftp://x.x.x.x/tmp/myfile.txt";
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverPath);
request.KeepAlive = true;
request.UsePassive = true;
request.UseBinary = true;
request.Method = WebRequestMethods.Ftp.DownloadFile;
request.Credentials = new NetworkCredential(username, password);
// Read the file from the server & write to destination
using (FtpWebResponse response = (FtpWebResponse)request.GetResponse()) // Error here
using (Stream responseStream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(responseStream))
using (StreamWriter destination = new StreamWriter(destinationFile))
{
destination.Write(reader.ReadToEnd());
destination.Flush();
}
Run Code Online (Sandbox Code Playgroud)
错误是:
远程服务器返回错误:(550)文件不可用(例如,找不到文件,没有访问权限)
该文件肯定存在于远程计算机上,我可以手动执行此ftp(即我有权限).任何人都可以告诉我为什么我可能会收到此错误?
我正在尝试在代码中通过 SSL 打开 FTP 连接。我可以使用 WinSCP 的 FileZilla 连接并列出目录。但是当使用 .NET 代码列出目录时FtpWebClient,我收到错误
(425) 无法打开数据连接
由于我能够从同一台计算机使用 FileZilla 进行连接,因此我不确定如何解决此问题。
这是我的代码
public void FtpStuff()
{
string url = "ftp://my.server.com";
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(url);
request.Credentials = new NetworkCredential("myname", "password");
request.EnableSsl = true;
request.Method = WebRequestMethods.Ftp.ListDirectory;
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
StreamReader streamReader = new StreamReader(response.GetResponseStream());
// This is the line that throws the exception
string line = streamReader.ReadLine();
}
Run Code Online (Sandbox Code Playgroud)
我也尝试过FluentFTP。这是我的代码。我得到了例外
无法建立数据连接:不允许操作。
public void FtpStuff()
{
FtpClient client = new FtpClient();
client.Host = "my.server.com";
client.Credentials …Run Code Online (Sandbox Code Playgroud) 在使用 .Net6 的 C# 中,我们可以使用 -object 与 FTP 服务器交互FtpWebRequest。这效果很好。但是我们应该如何创建这个对象呢?目前我们使用WebRequest.Create()
并且微软说我们应该这样做。
但这个功能现在已经过时了。因此,当Create-function 已过时但FtpWebRequest-type 未过时时,FtpWebRequest在不使用过时函数的情况下创建 -object 的正确且推荐的方法是什么?
我想在C#和Visual Studio 2010中创建一个FTP客户端.你能建议我一些好的链接开始吗?我想使用最新版本的C#,所以我正在寻找最新的代码示例以获得帮助,以便我可以从已经制作的代码中获益.
谢谢.