Link to home
Start Free TrialLog in
Avatar of Sheldon Livingston
Sheldon LivingstonFlag for United States of America

asked on

Download file using VB.NET

I use the following code to upload a file to an ftp server:

Dim clsRequest As System.Net.FtpWebRequest = DirectCast(System.Net.WebRequest.Create("ftp://www.myserver.com/mytextfile.txt"), System.Net.FtpWebRequest)
clsRequest.Credentials = New System.Net.NetworkCredential("user", "pw")
clsRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile

' read in file...
Dim bFile() As Byte = System.IO.File.ReadAllBytes("C:\text.txt")

' upload file...
Dim clsStream As System.IO.Stream = clsRequest.GetRequestStream()
clsStream.Write(bFile, 0, bFile.Length)
clsStream.Close()
clsStream.Dispose()

Anything that easy to download an exe file?
Avatar of kaufmed
kaufmed
Flag of United States of America image

Sure:
Dim clsRequest As System.Net.FtpWebRequest = DirectCast(System.Net.WebRequest.Create("ftp://www.myserver.com/mytextfile.txt"), System.Net.FtpWebRequest)
clsRequest.Credentials = New System.Net.NetworkCredential("user", "pw")
clsRequest.Method = System.Net.WebRequestMethods.Ftp.DownloadFile

' read in file...
Dim bFile() As Byte = System.IO.File.ReadAllBytes("C:\text.txt")

' upload file...
Dim clsStream As System.IO.MemoryStream = DirectCast(clsRequest.GetResponse(), System.Net.FtpWebResponse).GetResponseStream()
Dim outFile As New System.IO.FileStream("C:\your\new\path.txt", IO.FileMode.Create)
clsStream.WriteTo(outFile)

Open in new window

You can remove lines 5, 6, 7 & 8...  they are irrelevant to the code I posted  :)
http://msdn.microsoft.com/en-us/library/system.net.ftpwebrequest.aspx
Try this code.
 
 
 

' Get the object used to communicate with the server.
        Dim serverUri As Uri = New Uri("ftp://yourftpipaddress/folder")
        Dim request As WebClient = New WebClient()
        'This example assumes the FTP site uses anonymous logon.
        request.Credentials = New NetworkCredential("YourUserID", "Password")
        Try
            Dim newFileData() As Byte = request.DownloadData(serverUri.ToString())
            System.IO.File.WriteAllBytes(@"C:\Temp\yourfilename",newFileData)  
        Catch e As WebException
            MessageBox.Show(e.Message)
        End Try

Open in new window

Avatar of Sheldon Livingston

ASKER

The code is crashing at

Dim clsStream As System.IO.MemoryStream = DirectCast(clsRequest.GetResponse(), System.Net.FtpWebResponse).GetResponseStream()

with the error of

A first chance exception of type 'System.Net.WebException' occurred in System.dll

Is this because it is an exe file?
"First chance" exceptions don't cause applications to crash that I'm aware of--this is the runtime handling the exception for you. Are you getting a different exception?
Well... the exe isn't downloading...
It's more than likely the implicit cast to MemoryStream that I gave. This should be better:
Dim clsRequest As System.Net.FtpWebRequest = DirectCast(System.Net.WebRequest.Create("ftp://www.myserver.com/mytextfile.txt"), System.Net.FtpWebRequest)
clsRequest.Credentials = New System.Net.NetworkCredential("user", "pw")
clsRequest.Method = System.Net.WebRequestMethods.Ftp.DownloadFile

' read in file...
Dim bFile() As Byte = System.IO.File.ReadAllBytes("C:\text.txt")

' upload file...
Dim clsStream As System.IO.Stream = DirectCast(clsRequest.GetResponse(), System.Net.FtpWebResponse).GetResponseStream()
Dim outFile As New System.IO.FileStream("C:\your\new\path.txt", IO.FileMode.Create)
Dim str As New System.IO.MemoryStream()
Dim buffer(clsStream.Length) As Byte

str.Read(buffer, 0, buffer.Length)
str.WriteTo(outFile)

Open in new window

Closer!  Now I get a 0kb file with the correct file name.
Sorry...   forgot to close the stream  !!!   Doh!!!
Dim clsRequest As System.Net.FtpWebRequest = DirectCast(System.Net.WebRequest.Create("ftp://www.myserver.com/mytextfile.txt"), System.Net.FtpWebRequest)
clsRequest.Credentials = New System.Net.NetworkCredential("user", "pw")
clsRequest.Method = System.Net.WebRequestMethods.Ftp.DownloadFile

' read in file...
Dim bFile() As Byte = System.IO.File.ReadAllBytes("C:\text.txt")

' upload file...
Dim clsStream As System.IO.Stream = DirectCast(clsRequest.GetResponse(), System.Net.FtpWebResponse).GetResponseStream()
Dim outFile As New System.IO.FileStream("C:\your\new\path.txt", IO.FileMode.Create)
Dim str As New System.IO.MemoryStream()
Dim buffer(clsStream.Length) As Byte

str.Read(buffer, 0, buffer.Length)
str.WriteTo(outFile)

' This should flush the buffer to write the data to the file.
outFile.Close()
str.Close()
clsStream.Close()

Open in new window

Same thing...

Issue seems to be:

Dim buffer(clsStream.Length) As Byte

ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I have tested the below code to download a file from ftp server to your local PC.

Try it. Enter the ftp server ip, folder and file name details, user id and password in the code.






Imports System.Net

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim serverUri As Uri = New Uri("ftp://YourFtpIPAddress/FolderName/Test1.txt")
        Dim request As WebClient = New WebClient()
        request.Credentials = New NetworkCredential("FtpUserId", "FtpPassword")
        request.Proxy = Nothing

        Try
            Dim newFileData() As Byte = request.DownloadData(serverUri.ToString())
            System.IO.File.WriteAllBytes("D:\Temp\Test1.txt", newFileData)
        Catch ex As WebException
            MessageBox.Show(ex.Message)
        End Try
    End Sub
End Class

Open in new window

What about this?

        Dim wc As New System.Net.WebClient()
        wc.Credentials = New System.Net.NetworkCredential("user", "pw")
        wc.DownloadFile("ftp://www.myserver.com/mytextfile.txt", "C:\text.txt")

Wayne
Sweet!  Can't believe you got it as far as you did "from the hip".  Well done.

Thank you.
NP. Sorry it took so many posts, but glad to help nonetheless   :)