0

I am using FtpWebRequest to download files, but in all text files all \r\n are removed when downloaded.

What am I doing wrong?

Uri u = new Uri(msg);
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(u);

request.Method = WebRequestMethods.Ftp.DownloadFile;
request.Credentials = credential;
request.UsePassive = true;
request.UseBinary = true;
request.KeepAlive = false;

//Get a reponse
WebResponse response = request.GetResponse();
Stream responseStream = response.GetResponseStream();

FileStream localfileStream = new FileStream(destination,
    FileMode.Create, FileAccess.Write);
//create the file
byte[] buffer = new byte[1024];
int bytesRead = responseStream.Read(buffer, 0, 1024);

while (bytesRead != 0)
{
    localfileStream.Write(buffer, 0, bytesRead);
    bytesRead = responseStream.Read(buffer, 0, 1024);
}

localfileStream.Close();
response.Close();
responseStream.Close();. 
4

2 回答 2

0

你的代码是正确的。还有一些你没有告诉的导致问题的东西。我的猜测是

  • a) 服务器上的文件没有\r\n
  • b)您检查客户端上没有它们的方式是错误的。您正在检查错误的文件,或者它已经进行了额外的转换或其他。
于 2013-02-05T19:26:30.610 回答
0

我有一个类似的问题。我使用的代码与您的几乎相同。我发现我的代码实际上工作正常,但服务器上的文件只包含行尾的“LF”字符,而不是“CR/LF”组合。大多数文本编辑器会忽略这一点,并将文本显示为一条连续的线。

于 2013-05-06T09:07:15.920 回答