Saving files locally downloaded from the net

  • Thread starter Thread starter Xarky
  • Start date Start date
X

Xarky

Hi,
I am downloading data from the Internet using the following lines.

networkStream = tcpClient.GetStream(); // tcpClient specifies port +
ip
StreamReader streamReader = new StreamReader(networkStream);
response = streamReader.ReadLine();

After doing correct parsing of the downloaded code, I identify the
file extension such as txt, jpg, mp3, etc.

Now I would like to write the file locally, by creating the file and
storing the data retrieved. To write to the file I am using the
following.

StreamWriter streamWriter = new StreamWriter(path, false);
streamWriter.Writeline(data);

This technique has worked for text documents but is failing in other
files.

Can someone provide some help.
Thanks in Advance
 
Hi Xarky,

I believe the problem may be caused by reading single lines which doesn't
make sense for non text data.
I would use StreamReader.ReadToEnd, WebClient.DownloadFile or handle the
Stream directly and reading chunks of bytes.
 
Back
Top