Click here to Skip to main content
15,902,894 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am downloading file from FTP server and saving it into local system. I found that line break is removed during the transfer of file and text file is showing all line in single row.

I did following code. Please suggest.
C#
public void DownloadFTPFile()
        {

            string host = ConfigurationManager.AppSettings.Get("ftphost");
            string username = ConfigurationManager.AppSettings.Get("ftpusername");
            string password = ConfigurationManager.AppSettings.Get("ftppassword");

            string fileName = "Receipt.txt";
            try
            {
                string fullPath = host + "/FROM-SAP-2-MOB/" + fileName;
                //Create FTP Request.
                FtpWebRequest request = (FtpWebRequest)WebRequest.Create(fullPath);
                request.Method = WebRequestMethods.Ftp.DownloadFile;
                //Enter FTP Server credentials.
                request.Credentials = new NetworkCredential(username, password);
                request.UsePassive = true;
                request.UseBinary = true;
                request.EnableSsl = false;
                //Fetch the Response and read it into a MemoryStream object.
                FtpWebResponse response = (FtpWebResponse)request.GetResponse();
                using (Stream responseStream = response.GetResponseStream())
                {
                    using (Stream fileStream = new FileStream(Application.StartupPath.ToString() + "\\IMPORT_CSV\\" + fileName, FileMode.CreateNew))
                    {
                        responseStream.CopyTo(fileStream);
                    }
                }
            }
            catch (WebException ex)
            {
                throw new Exception((ex.Response as FtpWebResponse).StatusDescription);
            }
        }
Thanks,
Ravi Sharma

What I have tried:

I tried to google to resolve this issue but didn't find answer.
Posted
Updated 11-May-18 4:06am
v2
Comments
F-ES Sitecore 11-May-18 10:16am    
How do you know the line breaks are there on the file in the ftp server? Both the link Richard posted and this one are on the first page of a google search for "c# ftp removes line breaks".

If this is a file you uploaded yourself then maybe it was uploaded in ascii mode but you are downloading in binary. Or if the file was generated by a non-Windows OS then the line breaks you are looking for might not be there in the file so a binary download won't work.

1 solution

Quote:
request.UseBinary = true;

Binary mode transfers the file bytes verbatim. The source file was almost certainly created on a Linux or Mac system, which uses a different line-break character than Windows.

Set UseBinary to false if you're transferring text files. Or alternatively, read the files using a text editor which supports line-breaks from different platforms - eg: Notepad++[^].

FTP Binary and ASCII Transfer Types And The Case of Corrupt Files[^]
 
Share this answer
 
Comments
MadMyche 11-May-18 10:21am    
MS announced that the next version of Notepad will support as well
Sharma Ravi 14-May-18 0:58am    
Thanks Richard. It worked for me.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900