Solved

Downloaded text file from FTP is one long string

Posted on 2014-01-30
4
372 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 13

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now