所以我试图将一些上传自动化到ftp,但我无法让它工作。我所拥有的是(尝试创建一个文件夹):
private void button1_Click(object sender, EventArgs e)
{
FTPUpload(txtIP.Text, txtUName.Text, txtPWord.Text);
}
private void FTPUpload(string ftpAddress, string ftpUName, string ftpPWord)
{
FtpWebRequest ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpAddress + "/AUTO_TEST_FOLDER"));
ftpRequest.Credentials = new NetworkCredential(ftpUName, ftpPWord);
ftpRequest.Method = WebRequestMethods.Ftp.MakeDirectory;
WebResponse response = ftpRequest.GetResponse();
using (var resp = (FtpWebResponse)ftpRequest.GetResponse())
{
MessageBox.Show(resp.StatusCode.ToString());
}
我一直得到WebException未处理“远程服务器返回错误:(550)文件不可用(例如,找不到文件,没有访问权限)。”在WebResponse response = ftpRequest.GetResponse();
行。
有人可以帮助我吗?
我尝试了几个解决方案,包括How do I create a directory on ftp server using C#?的答案,但没有成功(即使复制/粘贴答案并输入我的ip / uname / pword也没有成功)。
答案 0 :(得分:2)
我设法让它与之合作:
private void FtpCreateFolder(string ftpAddress, string ftpUName, string ftpPWord)
{
WebRequest ftpRequest = WebRequest.Create("ftp://" + ftpAddress + "/AUTO_TEST_FOLDER");
ftpRequest.Method = WebRequestMethods.Ftp.MakeDirectory;
ftpRequest.Credentials = new NetworkCredential(ftpUName, ftpPWord);
}
我猜问题是使用FtpWebRequest ftpRequest = (FtpWebRequest)FtpWebRequest.Create(...)
。无论如何,谢谢,希望其他人觉得这很有用!
答案 1 :(得分:0)
我知道这是一个老线程,但我想我会投入2美分。在过去,我遇到了从FTP服务器获得成功或失败的问题,我发现的任何东西都比我从用于创建我的实际应用程序的测试程序中获得的效果更好。似乎最大的问题来自于在尝试创建文件夹时获得成功或失败的响应。另一个问题来自于有多种异常类型。我在不同的点上同时拥有WebException
和Exception
,因此我在代码中使用了一般Exception
。这对我有用。 OP的微小变化是使用(FtpWebRequest)WebRequest
而不是他们拥有的东西。
public static bool CreateFolder(string folder)
{
bool success = false;
System.Net.FtpWebRequest ftp_web_request = null;
System.Net.FtpWebResponse ftp_web_response = null;
string ftp_path = @"ftp://foo.bar.com/" + folder;
try
{
ftp_web_request = (FtpWebRequest)WebRequest.Create(ftp_path);
ftp_web_request.Method = WebRequestMethods.Ftp.MakeDirectory;
ftp_web_request.Credentials = new NetworkCredential("username", "password");
ftp_web_response = (FtpWebResponse)ftp_web_request.GetResponse();
string ftp_response = ftp_web_response.StatusDescription;
string status_code = Convert.ToString(ftp_web_response.StatusCode);
ftp_web_response.Close();
success = true;
}
catch (Exception Ex)
{
string status = Convert.ToString(Ex);
MessageBox.Show("Failed to create folder." + Environment.NewLine + status); //debug
}
return success;
}
以下是我用来上传多个文件的内容。我传入Dictionary
,其中键是文件的名称,值是路径。
public static bool UploadFile(string folder, Dictionary<string, string> Photo_Paths)
{
bool success = false;
FtpWebRequest ftp_web_request = null;
FtpWebResponse ftp_web_response = null;
foreach (KeyValuePair<string, string> item in Photo_Paths)
{
string subdomain = ConfigurationManager.AppSettings["subdomain"];
string ftp_path = @"ftp://foo.bar.com/" + folder + @"/" + item.Key;
try
{
ftp_web_request = (FtpWebRequest)WebRequest.Create(ftp_path);
ftp_web_request.UseBinary = true;
ftp_web_request.UsePassive = false;
ftp_web_request.EnableSsl = false;
ftp_web_request.Method = WebRequestMethods.Ftp.UploadFile;
ftp_web_request.Credentials = new NetworkCredential("username", "password");
try
{
MessageBox.Show(item.Value); //debug
byte[] buffer = File.ReadAllBytes(item.Value);
using (Stream file_stream = ftp_web_request.GetRequestStream())
{
file_stream.Write(buffer, 0, buffer.Length);
}
ftp_web_response = (FtpWebResponse)ftp_web_request.GetResponse();
if (ftp_web_response != null)
{
string ftp_response = ftp_web_response.StatusDescription;
string status_code = Convert.ToString(ftp_web_response.StatusCode);
MessageBox.Show(ftp_response + Environment.NewLine + status_code); //debug
}
}
catch (Exception Ex) //(WebException Ex)
{
//string status = ((FtpWebResponse)Ex.Response).StatusDescription;
string status = Convert.ToString(Ex);
MessageBox.Show("Failed upload a file." + Environment.NewLine + status); //debug
}
ftp_web_response.Close();
success = true;
}
catch (Exception Ex)
{
//string status = ((FtpWebResponse)Ex.Response).StatusDescription;
string status = Convert.ToString(Ex);
if (ftp_web_response != null)
{
string ftp_response = ftp_web_response.StatusDescription;
string status_code = Convert.ToString(ftp_web_response.StatusCode);
MessageBox.Show(ftp_response + Environment.NewLine + status_code); //debug
}
MessageBox.Show("Failed upload a file." + Environment.NewLine + status); //debug
}
}
return success;
}