Using FtpWebRequest to download a file from ftp server

bfsog

Well-known member
Joined
Apr 21, 2005
Messages
50
Location
UK
Programming Experience
5-10
Hello.

I am trying to download a file which is on an FTP Server.

Lets say I own Website A and Website B. In Site A in a folder named foobar I have a file named download.aspx and in that file I want to have code that downloads a file in Website B called lorem.txt

I want the file to be saved into the foobar directory in Website A.

Is this possible?

So far, my code seems to download (as the contents of the file I download is displayed) but there is no sign of the file. Where does it go?

VB.NET:
    Public Sub ftpDownload(ByVal filePath As String, ByVal fileName As String)
        Dim reqFTP As FtpWebRequest
        Try 
            Dim file As String = (filePath + fileName)
            Dim request As FtpWebRequest = CType(WebRequest.Create(file),FtpWebRequest)
            request.Method = WebRequestMethods.Ftp.DownloadFile
            request.Credentials = New NetworkCredential("user", "pass")
            Dim response As FtpWebResponse = CType(request.GetResponse,FtpWebResponse)
            Dim ftpStream As Stream = response.GetResponseStream
            Dim reader As StreamReader = New StreamReader(ftpStream, System.Text.Encoding.UTF8)
            output.Text = reader.ReadToEnd
            output.Text = (output.Text + "Download Complete")
        Catch ex As Exception
            output.Text = ("Error: " + ex.ToString)
        End Try
    End Sub

The code "works" but the file does not get copied/downloaded, where is the file being downloaded to?
 
Last edited by a moderator:
This is a VB.Net forum site, I translated your C# post to VB.Net. Take care!
 
While I appreciate that this is primarily a VB.NET website, ASP.NET is not tied to one language. But thanks, if it helps people provide a solution then its all good.
 
VB.Net Forums is tied to VB.Net related matters only. We use VB.Net when developing ASP.Net solutions. Thank you.
 
Back
Top