Solved

Downloaded text file from FTP is one long string

Posted on 2014-01-30
4
379 Views
Last Modified: 2014-01-31
Hello,

I have code that is working correctly to download a text file from an FTP server, however that text file comes across as one big long run-on string.  The text file I'm downloading is a listing of access codes delimited by a carriage return, which i eventually need to modify and re-upload, so the file needs to stay in-tact in regards to the formatting of it.  Is there any way to download the file as-is and not have it return as a big pile of characters?  This is my first attempt at pulling down a file from FTP so I'm hoping there is just something easy I am missing.

The code I am using currently to get the file and save it is:

string serverpath = "ftp://ftpsite.com/codefile.txt";

FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverpath);
request.Method = WebRequestMethods.Ftp.DownloadFile;
request.Credentials = new NetworkCredential("username", "password");

FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);

StreamWriter writer = new StreamWriter("Y:\\downloadedCodeFile.txt");
writer.Write(reader.ReadToEnd());

writer.Close();
reader.Close();
response.Close();

Thanks,
Joe
0
Comment
Question by:sglew
  • 2
4 Comments
 
LVL 16

Expert Comment

by:AlexPace
ID: 39822692
Try transferring the file in ASCII mode.  That will cause the server to convert the line endings to CRLF so they will appear "normal" in Windows.  Do it when you re-upload too so the server can translate the line endings back into its native format.
0
 
LVL 14

Expert Comment

by:frankhelk
ID: 39823633
Hmm - how deep have you inspected the resulting file ?

Does it still contain the original CR characters (which might be suppressed in display by your editor) or did those CRs stripped ?

If the characters are still in it, I would try to read / write the data not as a big blob but line by line, which might need possibly with some kind of end-of-line detection setting.

If the data file is not that big, I would try another attempt:
Transfer the data in binary mode into a big String object,

try to detect the EOL mode (CR / CR-LF) by searching for CR-CF in it,
then use String.Split(...) to make an array on Strings as Lines from it and finally
use File.WriteAll() to put the data into a disk file
Not elegant, but handy.
0
 
LVL 16

Accepted Solution

by:
AlexPace earned 400 total points
ID: 39823645
Don't forget that he has to re-upload the changed files so all that would have to be done in reverse after doing whatever modifications he does.

The FTP specification says that servers are responsible for changing the line endings to CRLF for transfers done in ASCII mode and almost all servers are compliant in that regard...  Transferring in binary and manually re-inflating the line endings is certainly an option but it seems like a waste of time when you could just do the transfer in ASCII in the first place by doing something like this:
request.UseBinary = false;

... anyway, while on the topic of "not elegant, but handy" .... you could download copies of the unix2dos and dos2unix utilities... save the string as a file... run unix2dos on it to get your line endings back... do your modifications... run dos2unix to change the line endings back to a format that the remote site will understand... then upload it in binary.  heh,
0
 

Author Closing Comment

by:sglew
ID: 39824021
Bingo, that was it!  I figured it was probably something easy that I was missing.  Setting the UseBinary attribute to false allowed the file to save as expected.

Thanks AlexPace.

-Joe
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question