传输文本文件后,其中会出现空白行
编辑:Filezilla 导致了问题,当我从服务器下载文件时,它添加了新行。很抱歉造成混乱。
此方法将文件上传到ftp服务器并且工作正常,但是在上传到服务器的文本文件中,每行后都会出现空行(出现“cr lf”),例如:
File:
First line
Second line
Third line
Uploaded file:
First line
Second line
Third line
Origin和上传的文件相应地具有不同的大小,非文本文件是相同。
代码:
private void sendFile(string In, string Out)
{
FtpWebRequest request = (FtpWebRequest) WebRequest.Create("ftp://domain//" + Out);
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential("username", "password");
FileStream sourceStream = new FileStream(In, FileMode.Open, FileAccess.Read, FileShare.Read);
byte[] fileContents = new byte[sourceStream.Length];
sourceStream.Read(fileContents, 0, (int) sourceStream.Length);
sorceStream.Close();
request.ContentLength = fileContents.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
}
我该如何解决这个问题?
EDIT: Filezilla caused the problem, when i download files back from server it added new lines. I'm sorry for confusion.
This method upload files to ftp server and it's work fine, but in text files uploaded to server blank lines appear after every line("cr lf" appear), for example:
File:
First line
Second line
Third line
Uploaded file:
First line
Second line
Third line
Origin and uploaded files accordingly have different sizes, non-text files are the same.
Code:
private void sendFile(string In, string Out)
{
FtpWebRequest request = (FtpWebRequest) WebRequest.Create("ftp://domain//" + Out);
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential("username", "password");
FileStream sourceStream = new FileStream(In, FileMode.Open, FileAccess.Read, FileShare.Read);
byte[] fileContents = new byte[sourceStream.Length];
sourceStream.Read(fileContents, 0, (int) sourceStream.Length);
sorceStream.Close();
request.ContentLength = fileContents.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
}
How can i fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
编辑:由于下面的答案似乎没有帮助(但我将其留在那里供后代使用,因为它显示了更好的代码),这是我要检查的下一个诊断步骤:
您应该设置
FtpWebRequest.UseBinary
为 true 以保留确切的文件内容。否则,两个系统将尝试自行找出行结束符,并根据需要更改行结束符。我很少认为这是一个好主意。 (编辑:默认情况下,UseBinary
实际上是true
,但这听起来像是使用文本模式引入的问题......它确实如此明确这一点没有什么坏处。)另外:
using
语句处置您的FileStream
using< 处置请求流/code> 语句
Stream.Read
- 它不需要总是一次性读取整个请求的数据File.ReadAllBytes
一次性读取完整的文件数据,或者使用Stream.CopyTo
(如果您使用的是 .NET 4)将FileStream
复制到请求流(当然,这不会设置内容长度;我不知道这是否是一个问题)GetResponse
;目前还不清楚如果您从不获取 FtpWebRequest 的响应会发生什么所以我可能会使用:
EDIT: As the answer below doesn't seem to have helped (but I'm leaving it there for posterity as it shows better code) here are the next diagnostics steps I'd check:
You should set
FtpWebRequest.UseBinary
to true in order to preserve the exact file contents. Otherwise the two systems will try to figure out line endings themselves, changing line terminators as they see fit. I very rarely think that's a good idea. (EDIT:UseBinary
is actuallytrue
by default, but this sounds like the kind of problem introduced by using text mode... it certainly does no harm to make this explicit.)Additionally:
FileStream
via ausing
statementusing
statementStream.Read
- it needn't always read the whole of the requested data in one goFile.ReadAllBytes
to simply read the complete file data in one go, or useStream.CopyTo
(if you're using .NET 4) to copy theFileStream
to the request stream (which won't set the content length, of course; I don't know whether this is a problem)GetResponse
; it's unclear exactly what happens if you never fetch the response of anFtpWebRequest
So I would probably use:
很奇怪。我面临同样的问题,直到我没有在文件中提供扩展名之前我无法修复它。例如,如果我的文件名是
abcfile
,那么我将其设为 abcfile.dat,然后它会将上传的文件显示为实际文件。我再次使用 abcfile.txt 上传文件,但这一次我上传的文件中再次出现空行问题。
我建议您必须为文件提供除 .txt 之外的任何扩展名。
Its strange. I face the same problem and I was unable to fix it until I did not provide an extension in file. For Example if my file name was
abcfile
then I make it abcfile.dat and after that it shows me the uploaded file as actual file. I again upload file with abcfile.txt but this time again empty line problem appear in my uploaded file.
I suggest that you must provide extension to your file any except .txt.
您要发送到的系统使用的行结束符与您的系统使用的行结束符不同。我可以假设,因为您得到了额外的一行,所以您使用的是 Windows,并且它使用 CRLF 结尾。您发送到的系统将 CR 和 LF 识别为单独的结尾,因此您会得到额外的行。
对于文本,截断 LF 或 CR,看看会发生什么。我不知道不同的文件大小。
The system that you're sending to uses different line endings to what your system uses. I can assume, because you get an extra line, that you're on Windows, and it uses CRLF endings. The system you're sending to recognises CR and LF as separate endings, so you get the extra lines.
For text, truncate the LF or the CR, see what happens. I have no clue about the differing file sizes.
在FileZilla的顶部菜单中,设置:
In the top menu of FileZilla, set:
在 FileZilla 的顶部菜单中,设置:
它对我有用。
In the top menu of FileZilla, set:
It's working for me.