0

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?

Anatoly
  • 25
  • 1
  • 5
  • 1
    Unrelated, but you should really wrap your streams around `using` blocks to ensure they're disposed properly. – Joe Enos Aug 07 '11 at 18:19

5 Answers5

2

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.

2

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())
    {
    }
}
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • That was the first thing I thought of as well, but I noticed in the documentation that the default value is already true. – Joe Enos Aug 07 '11 at 18:25
  • @Joe: Hmm... you're right. I would personally set it to True explicitly to see if that makes a difference... but there are enough other problems in the OP's code that it's probably worth using the new version shown in my answer, and then checking out the results. Of course it's possible that the OP's FTP server is broken... – Jon Skeet Aug 07 '11 at 18:27
  • @Jon Skeet: Used your code, but problem persists. Also thank you for correcting! – Anatoly Aug 07 '11 at 18:39
  • @Anatoly: I've edited my answer with some more diagnostic steps at the start. Basically they're where I'd look next... – Jon Skeet Aug 07 '11 at 18:43
1

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.

Steffan Donal
  • 2,244
  • 4
  • 24
  • 47
0

In the top menu of FileZilla, set:

Transfer menu > Transfer type > binary
Zeke
  • 562
  • 4
  • 14
0

In the top menu of FileZilla, set:

Transfer menu > Transfer type > binary

It's working for me.

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 03 '21 at 06:57