【发布时间】:2013-09-25 12:27:03
【问题描述】:
我正在尝试将文件上传到 ftp 服务器。尝试了一些代码示例,但总是出现此错误,进入被动模式。例如,我可以用这段代码创建一个目录
FtpWebRequest reqFTP;
try
{
// dirName = name of the directory to create.
reqFTP = (FtpWebRequest)FtpWebRequest.Create(
new Uri("ftp://" + ftpServerIP + "/" + dirName));
reqFTP.Method = WebRequestMethods.Ftp.MakeDirectory;
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
reqFTP.UsePassive = false;
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Stream ftpStream = response.GetResponseStream();
ftpStream.Close();
response.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
或者例如我可以重命名一个文件。但无法使用此代码上传文件
string uri = "ftp://" + ftpServerIP + "/" + fileInf.Name;
FtpWebRequest reqFTP;
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(
"ftp://" + ftpServerIP + "/" + fileInf.Name));
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
reqFTP.KeepAlive = false;
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
reqFTP.UseBinary = true;
reqFTP.ContentLength = fileInf.Length;
int buffLength = 2048;
byte[] buff = new byte[buffLength];
int contentLen;
FileStream fs = fileInf.OpenRead();
try
{
Stream strm = reqFTP.GetRequestStream();
contentLen = fs.Read(buff, 0, buffLength);
while (contentLen != 0)
{
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);
}
strm.Close();
fs.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message, "Upload Error");
}
在reqFTP.GetRequestStream() 出现异常。
如果我使用reqFTP.UsePassive=false,那么我会得到“
远程服务器返回错误:(500) 语法错误,命令无法识别”。
我该怎么办?
【问题讨论】:
-
任何有此问题的人,append a log 到该问题(或发布新问题并在此处链接)。没有日志,问题就无法回答。
-
由于卡巴斯基杀毒软件,我今天遇到了这个问题。如果我暂停卡巴斯基并重试一切正常
标签: c# ftp ftpwebrequest