0
我正在构建一个将.txt文件上传到FTP服务器的简单应用程序。我之前已经完成了这一步,并且使用了与其他应用程序相同的代码。将文本文件上传到FTP服务器C时出错#
这是我的代码:
string localFilePath = @"\\fileprint\data\Groups\Operation\fileExports\dls\";
string archiveFilePath = @"\\fileprint\data\Groups\Operation\fileExports\dls\Archive\";
string logFilePath = @"C:\Users\lmy\Desktop\Logs";
string ftpServer = "ftp://server:21/home/out2233/tmp";
private string logFileName = "" + DateTime.Now.Year.ToString() + "-" + DateTime.Now.Month.ToString() + "-" + DateTime.Now.Day.ToString();
public void UploadFile()
{
try
{
string[] files = Directory.GetFiles(localFilePath);
foreach (string file in files)
{
string fileName = Path.GetFileName(file);
string modified = file.Remove(60, 6);
string modifiedFile = Path.GetFileName(modified);
FtpWebRequest ftpReq = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpServer + modifiedFile));
ftpReq.Method = WebRequestMethods.Ftp.UploadFile;
ftpReq.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
ftpReq.UsePassive = true;
ftpReq.UseBinary = true;
ftpReq.KeepAlive = true;
ftpReq.Credentials = new NetworkCredential("out2233", "password");
ftpReq.EnableSsl = true;
FileInfo fileInfo = new FileInfo(localFilePath + @"\" + fileName);
FileStream fileStream = fileInfo.OpenRead();
byte[] fileContent = new byte[fileInfo.Length];
fileStream.Read(fileContent, 0, Convert.ToInt32(fileInfo.Length));
Stream writer = ftpReq.GetRequestStream();
writer.Write(fileContent, 0, fileContent.Length);
fileStream.Close();
writer.Close();
FtpWebResponse response = (FtpWebResponse)ftpReq.GetResponse();
AppendLogFile(response, "Uploaded Files: ", fileName);
MoveToArchive(file, archiveFilePath + fileName);
}
}
catch (Exception exception)
{
Console.WriteLine(exception.Message);
}
}
但得到这个错误:{“无法发送内容体与这个动词型”} 例外=
当代码达到这一行:
Stream writer = ftpReq.GetRequestStream();
我已经使用了这个,但我只能找到ASP的例子。我似乎无法找到我在这里做错了什么。希望你们能帮助我。
的感谢!
这给了我一个新的错误:exception = {“remote s erver返回了一个错误:(530)Not logged in。“} 上午输入我的凭据是错误的吗? – Lahib
@Lahib,从这个答案尝试解决方案:http://stackoverflow.com/questions/10352165/using-ftpwebrequest-with-an-error-the-remote-server-returned-error-530-not-loge – Dmytro