传输文本文件后,其中会出现空白行

发布于 2024-11-28 05:46:03 字数 1073 浏览 2 评论 0原文

编辑: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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

信仰 2024-12-05 05:46:03

编辑:由于下面的答案似乎没有帮助(但我将其留在那里供后代使用,因为它显示了更好的代码),这是我要检查的下一个诊断步骤:

  • 您如何查看文件?如果可能的话,直接访问服务器,而不是通过网络浏览器或其他方式再次获取文件。
  • 您连接的 FTP 服务器是什么类型?也许有一个已知问题。
  • 您是否尝试过查看通过 Wireshark 实际发送的内容?
  • 您是否尝试过通过普通 FTP 客户端发送相同的文件?

您应该设置 FtpWebRequest.UseBinary 为 true 以保留确切的文件内容。否则,两个系统将尝试自行找出行结束符,并根据需要更改行结束符。我很少认为这是一个好主意。 (编辑:默认情况下,UseBinary 实际上是 true,但这听起来像是使用文本模式引入的问题......它确实如此明确这一点没有什么坏处。)

另外:

  • 您应该通过 using 语句处置您的 FileStream
  • 您应该通过 using< 处置请求流/code> 语句
  • 您应该注意以下结果Stream.Read - 它不需要总是一次性读取整个请求的数据
  • 您可以使用 File.ReadAllBytes 一次性读取完整的文件数据,或者使用 Stream.CopyTo (如果您使用的是 .NET 4)将 FileStream 复制到请求流(当然,这不会设置内容长度;我不知道这是否是一个问题)
  • 您永远不会调用 GetResponse;目前还不清楚如果您从不获取 FtpWebRequest 的响应会发生什么
  • 您的参数名称与 .NET 命名约定不匹配,并且描述性不强

所以我可能会使用:

private void SendFile(string inputFile, string outputPath)
{   
    FtpWebRequest request = (FtpWebRequest) WebRequest.Create
        ("ftp://domain//" + outputPath);
    request.Method = WebRequestMethods.Ftp.UploadFile;
    request.UseBinary = true;
    request.Credentials = new NetworkCredential("username", "password");

    byte[] fileContents = File.ReadAllBytes(inputFile);
    request.ContentLength = fileContents.Length;

    using (Stream requestStream = request.GetRequestStream())
    {
        requestStream.Write(fileContents, 0, fileContents.Length);
    }

    // This *may* be necessary in order to validate that everything has happened
    using (WebResponse response = request.GetResponse())
    {
    }
}

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:

  • How are you viewing the files? If at all possible, get onto the server directly rather than fetching the files again via a web browser or whatever.
  • What's the type of FTP server you're connecting to? Maybe there's a known issue.
  • Have you tried looking at what's actually being sent via Wireshark?
  • Have you tried sending the same files via a normal FTP client?

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 actually true 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:

  • You should be disposing of your FileStream via a using statement
  • You should be disposing of the request stream via a using statement
  • You should be taking note of the result of Stream.Read - it needn't always read the whole of the requested data in one go
  • You can either use File.ReadAllBytes to simply read the complete file data in one go, or use Stream.CopyTo (if you're using .NET 4) to copy the FileStream to the request stream (which won't set the content length, of course; I don't know whether this is a problem)
  • You're never calling GetResponse; it's unclear exactly what happens if you never fetch the response of an FtpWebRequest
  • Your parameter names don't match .NET naming conventions, and aren't very descriptive

So I would probably use:

private void SendFile(string inputFile, string outputPath)
{   
    FtpWebRequest request = (FtpWebRequest) WebRequest.Create
        ("ftp://domain//" + outputPath);
    request.Method = WebRequestMethods.Ftp.UploadFile;
    request.UseBinary = true;
    request.Credentials = new NetworkCredential("username", "password");

    byte[] fileContents = File.ReadAllBytes(inputFile);
    request.ContentLength = fileContents.Length;

    using (Stream requestStream = request.GetRequestStream())
    {
        requestStream.Write(fileContents, 0, fileContents.Length);
    }

    // This *may* be necessary in order to validate that everything has happened
    using (WebResponse response = request.GetResponse())
    {
    }
}
清晨说晚安 2024-12-05 05:46:03

很奇怪。我面临同样的问题,直到我没有在文件中提供扩展名之前我无法修复它。例如,如果我的文件名是

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.

小女人ら 2024-12-05 05:46:03

您要发送到的系统使用的行结束符与您的系统使用的行结束符不同。我可以假设,因为您得到了额外的一行,所以您使用的是 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.

剑心龙吟 2024-12-05 05:46:03

在FileZilla的顶部菜单中,设置:

Transfer menu > Transfer type > binary

In the top menu of FileZilla, set:

Transfer menu > Transfer type > binary
幸福%小乖 2024-12-05 05:46:03

在 FileZilla 的顶部菜单中,设置:

Transfer menu > Transfer type > binary

它对我有用。

In the top menu of FileZilla, set:

Transfer menu > Transfer type > binary

It's working for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文