Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: 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 8:09am
lachu88357
Edited 19-Sep-12 8:38am
Mehdi Gholam259.4K
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 at 20-Sep-12 11:30am
   
Sir!!!!
 
Thanks
 
PLz Tell me clearly!!!!
Kuthuparakkal at 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 at 20-Sep-12 11:33am
   
Thanks I will try it!!!!
lachu88 at 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
0 Zoltán Zörgő 200
1 Sergey Alexandrovich Kryukov 160
2 Peter Leow 90
3 Ramza360 51
4 Arkadeep De 50
0 Zoltán Zörgő 80
1 the other paul 23
2 Shmuel Zang 10
3 Aarti Meswania 5
4 Member 11389991 -2


Advertise | Privacy | Mobile
Web03 | 2.8.150129.1 | Last Updated 20 Sep 2012
Copyright © CodeProject, 1999-2015
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