gorbit Posted May 2, 2007 Posted May 2, 2007 I'm writing a sub in one of my VB programs for downloading .exe files from an HTTP server to the local machine. But when ever the file is downloaded it appears to be corrupted because no icon shows on the file and it wont run. The same code works fine for ordinary HTML pages but not exes it seems. Can anyone help with this problem? - here's the sub: Dim v_base As String = "http://www.talbotech.com/" Public Sub GetFile(ByVal uri As String, ByVal toFile As String) Dim wreq As Net.HttpWebRequest = Net.WebRequest.CreateDefault(New Uri(v_base & uri)) wreq.AllowAutoRedirect = True wreq.Proxy = Net.WebProxy.GetDefaultProxy Dim resI As Net.HttpWebResponse = wreq.GetResponse Dim wres As IO.StreamReader = New IO.StreamReader(resI.GetResponseStream, System.Text.Encoding.Default) Dim buff(resI.ContentLength - 1) As Char wres.Read(buff, 0, buff.Length) Dim fileIO As IO.StreamWriter = New IO.StreamWriter(IO.File.Open(toFile, IO.FileMode.Create), System.Text.Encoding.Default) fileIO.Write(buff, 0, buff.Length) fileIO.Close() wres.Close() resI.Close() End Sub Quote
MrPaul Posted May 2, 2007 Posted May 2, 2007 Executables are not text The problem is almost certainly to do with the fact that you're treating the response as text. I suggest you try reading directly from the Stream rather than using a StreamReader: Dim stm As Stream Dim buff As Byte() 'Read from the stream stm = resI.GetResponseStream() ReDim buff(stm.Length - 1) stm.Read(buff, 0, buff.Length) stm.Close() 'Write to the file stm = New FileStream(toFile, FileMode.Create) stm.Write(buff, 0, buff.Length) stm.Close() Good luck :) Quote Never trouble another for what you can do for yourself.
gorbit Posted May 2, 2007 Author Posted May 2, 2007 It didn't work, mate. I got rid of "fileIO" and just used "stm" as an io.stream an i still got the same result. can't run the file. i know the problem's not with the server either - it'll work if i use internet explorer to download it. the file on the server and the one downloaded using the sub are exactly the same size and even when i open the two in notepad, i see the same gibberish in each. i have no idea what is going on. here's the new sub Public Sub GetFile(ByVal uri As String, ByVal toFile As String) Dim wreq As Net.HttpWebRequest = Net.WebRequest.CreateDefault(New Uri(v_base & uri)) wreq.AllowAutoRedirect = True wreq.Proxy = Net.WebProxy.GetDefaultProxy Dim resI As Net.HttpWebResponse = wreq.GetResponse Dim stm As IO.Stream = resI.GetResponseStream Dim buff(resI.ContentLength - 1) As Byte stm.Read(buff, 0, buff.Length) stm.Close() stm = IO.File.Open(toFile, IO.FileMode.Create) stm.Write(buff, 0, buff.Length) stm.Close() resI.Close() End Sub Quote
Leaders dynamic_sysop Posted May 7, 2007 Leaders Posted May 7, 2007 there are a couple of examples on downloading files in the codebank, my example was used to download an image, i've simpley replaced the image name with an exe ( in this case windows messenger live on microsoft's site ) with it i downloaded the complete exe which you can launch. Private Sub download() Try Dim exepath As String = "[url]http://download.microsoft.com/download/1/A/4/1A4FEB1A-18E0-423A-B898-F697402E4F7F/Install_Messenger_nous.exe[/url]" '/// "[url]http://g.msn.com/8reen_us/EN_NOUS/INSTALL_MSN_MESSENGER_DL.EXE[/url]" Dim wRequest As HttpWebRequest = DirectCast(HttpWebRequest.Create(exepath), HttpWebRequest) Dim wResponse As WebResponse = DirectCast(wRequest.GetResponse(), WebResponse) Dim sWriter As FileStream = New FileStream("c:\msgr.exe", FileMode.OpenOrCreate) Dim ContentLength As Integer = wResponse.ContentLength Dim sizeInKB As String = (ContentLength / 1024).ToString() Me.Text = "The Size of the file is: " & sizeInKB.Substring(0, sizeInKB.IndexOf(".") + 3) & "KB" Dim buffer(ContentLength) As Byte Dim length As Integer = ContentLength Dim position As Integer = 0 Dim complete As Integer = 1 Dim returned As Integer = 0 ProgressBar1.Value = 0 '/// clear the progressbar. ProgressBar1.Maximum = ContentLength '/// set it's length. While Not complete = 0 position = wResponse.GetResponseStream().Read(buffer, returned, length) sWriter.Write(buffer, returned, position) complete = position returned += position length -= position ProgressBar1.Step = returned ProgressBar1.PerformStep() End While Me.Text = "Completed download" sWriter.Close() wRequest = Nothing Catch ex As Exception Console.WriteLine(ex.Message) Catch webex As WebException Console.WriteLine(webex.Message) End Try End Sub Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.