Click here to Skip to main content
12,446,296 members (54,341 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# C#4.0
try
            {
                HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(uri);
                httpRequest.Credentials = CredentialCache.DefaultCredentials;
                
                HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse();
 
                System.IO.Stream dataStream = httpResponse.GetResponseStream();
                System.IO.StreamReader streamReader = new System.IO.StreamReader(dataStream);
 
                String data = streamReader.ReadToEnd();
                
                byte[] buffer = new byte[data.Length];
 
                for (int i = 0; i < data.Length; i++)
                    buffer[i] = (byte)data[i];
                
                dataStream.Close();
                streamReader.Close();
                
                FileStream fs = new FileStream("filename.txt", FileMode.CreateNew);
                BinaryWriter bw = new BinaryWriter(fs);
 
                for (int i = 0; i < buffer.Length; i++)
                    bw.Write(buffer[i]);
 
                streamReader.Close();
                httpResponse.Close();
            }
            catch (Exception pe)
            {
                System.Windows.Forms.MessageBox.Show(pe.Message);
            }
This is code is working, when downloading a text,word and excel files, if i am Downloading a .cab or .exe files means it shows an exception "Value out of range" Actually i want to download the .cab files and .exe files, How to Download That???
Thanks
Posted 19-Sep-12 7:09am
lachu88357
Updated 19-Sep-12 7:38am
Mehdi Gholam319.2K
v3

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

you cannot convert binary files to string and then write it to file.

using (FileStream destination = File.Open(@"some.exe",FileMode.CreateNew))
{
dataStream.CopyTo(destination);
}
  Permalink  
v3
Comments
lachu88 20-Sep-12 11:30am
   
Sir!!!!

Thanks

PLz Tell me clearly!!!!
Kuthuparakkal 20-Sep-12 11:33am
   
try

{
HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(uri);
httpRequest.Credentials = CredentialCache.DefaultCredentials;

HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse();

System.IO.Stream dataStream = httpResponse.GetResponseStream();
using (FileStream destination = File.Open(@"some.exe",FileMode.CreateNew))

{
dataStream.CopyTo(destination);
}
httpResponse.Close();
}
catch (Exception pe)
{
System.Windows.Forms.MessageBox.Show(pe.Message);
}
lachu88 20-Sep-12 11:33am
   
Thanks I will try it!!!!
lachu88 20-Sep-12 11:44am
   
Thank You!!! Let me Try This Tomorrow !!!!

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160811.3 | Last Updated 20 Sep 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100