Use FTP from asp .net

Hello,

When a user clicks a button I want to ftp a file to some location from asp.net code behind using vb.  How do I do this in .net 2003?

Please show code.

Thanks
dmontgomAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AerosSagaCommented:
http://www.freevbcode.com/ShowCode.asp?ID=4655

source code available for download
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AerosSagaCommented:
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

AerosSagaCommented:
0
AerosSagaCommented:
In the earlier example, the FtpStream class is used to handle the data stream that is delivered to the server together with the request, as shown in the following sample code:

  Public Class FtpWebResponse
        Inherits WebResponse
        Public Overrides Property ContentType() As String
            Get
                'Override
            End Get
            Set(ByVal Value As String)
                'Override
            End Set
        End Property

        Public Overrides Function GetResponseStream() As Stream
          'Override
        End Function
  End Class

Similarly, by using WebRequest you can manage the details of a request to an Internet resource. This sample code uses the FtpWebRequest class that inherits from the WebRequest class. The WebRequest.Create method creates an FTP WebRequest instance based on the URI class that is passed. The instance that is returned is a typecast to the FtpWebRequest class to access protocol-specific properties. Typically, the WebRequest instance provides all the necessary information to formulate a request.

The following sample code demonstrates the methods that are implemented or overridden in the FtpWebRequest class. For more information and implementation, visit the Download Center for the VbFtpClient.exe file that is mentioned earlier in this article.

Public Class FtpWebRequest
        Inherits WebRequest

        Public Overrides Property Method() As String
            Get
                'Override
            End Get
            Set(ByVal Value As String)
                'Override
            End Set
        End Property

        Public Overrides Property Credentials() As ICredentials
            Get
                'Override
            End Get
            Set(ByVal Value As ICredentials)
                'Override
            End Set
        End Property

        Public Overrides Property ConnectionGroupName() As String
            Get
                'Override
            End Get
            Set(ByVal Value As String)
                'Override
            End Set
        End Property

        Public Overrides Property ContentLength() As Long
            Get
                'Override
            End Get
            Set(ByVal Value As Long)
                'Override
            End Set
        End Property

        Public Overrides Property ContentType() As String
            Get
                'Override
            End Get
            Set(ByVal Value As String)
                'Override
            End Set
        End Property


        Public Overrides Property Proxy() As IWebProxy
            Get
                'Override
            End Get
            Set(ByVal Value As IWebProxy)
                'Override
            End Set
        End Property
        Public Overrides Function GetRequestStream() As Stream
           'Override
        End Function

        Public Overrides Function GetResponse() As WebResponse
          'Override
        End Function
 End Class


back to the top
Create VbFtpClient
The vbFtpClient application accesses the information from the Internet by using a request-response model. You can retrieve protocol-specific information by using the WebRequest and WebResponse classes. To retrieve protocol-specific information, register the descendants of WebRequest by using the WebRequest.RegisterPrefix static method. The IWebRequestCreate interface defines the method that WebRequest descendants will use to register with the WebRequest.Create method.

The following sample code demonstrates how to use IWebRequestCreate:

    Public Class FtpRequestCreator
        Implements IWebRequestCreate
        Public Sub New()
        End Sub
        Public Overridable Function Create(ByVal Url As Uri) As WebRequest Implements IWebRequestCreate.Create
            Return New FtpWebRequest(Url)
        End Function
    End Class

The following sample code demonstrates how the RegisterPrefix method is used and how to create a WebRequest instance:

       ' FtpRequestCreator class implements IWebRequestCreate class, which implements Create method.
        Dim Creator As FtpRequestCreator = New FtpRequestCreator()
        WebRequest.RegisterPrefix("ftp:", Creator)
        Dim szUri As String = New String("ftp://localhost")

      ' Create WebRequest.
      Dim w As WebRequest = WebRequest.Create(szUri)

The WebRequest.RegisterPrefix method registers the class and notifies the descendants to use the FTP protocol for retrieving the data. After registration, descendants of WebRequest are created by using the WebRequest.Create method with an argument passed as URI. The WebRequest instance exposes properties, such as GetResponse, that control the request to the server and access to the data stream that is sent to the server. The GetResponse method of the WebRequest instance sends the request from the client application to the server that is identified in the URI.

     Dim r As WebResponse = w.GetResponse()
     Dim respstream As Stream = r.GetResponseStream()
     If (respstream.CanRead) Then
            Dim rdr As StreamReader = New StreamReader(respstream)
            Dim resp As String = rdr.ReadToEnd()
            rdr.Close()
            Console.WriteLine(resp)
     End If
 

The GetResponse method returns a WebResponse instance. The WebResponse provides access to the data that is returned by the server in the form of a stream returned by the GetResponseStream method. This stream can be used and modified in an application. You can derive a class from the stream class and override the method, based on application requirements.

For more information, visit the Download Center for the vbFtpClient.exe file that is mentioned earlier in this article.

back to the top
Steps to Run the VbFtpClient Executable
1.      Download the vbFtpClient.exe file that is mentioned earlier in this article.
2.      Extract the file.
3.      Open the Readme.htm file in the browser.
4.      To read the steps to build the application, click Building the sample.
5.      To read the steps to run the application, click Running the sample.
0
AerosSagaCommented:
Module Module1

' Declare the API functions
' The three that you will use are FtpGetFile (for downloading), FtpPutFile (for uploading), and InternetConnect (to specify the server and login credentials)
    Private Declare Function InternetCloseHandle Lib "wininet.dll" (ByVal HINet As Integer) As Integer
    Private Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" (ByVal sAgent As String, ByVal lAccessType As Integer, ByVal sProxyName As String, ByVal sProxyBypass As String, ByVal lFlags As Integer) As Integer
    Private Declare Function InternetConnect Lib "wininet.dll" Alias "InternetConnectA" (ByVal hInternetSession As Integer, ByVal sServerName As String, ByVal nServerPort As Integer, ByVal sUsername As String, ByVal sPassword As String, ByVal lService As Integer, ByVal lFlags As Integer, ByVal lContext As Integer) As Integer
    Private Declare Function FtpGetFile Lib "wininet.dll" Alias "FtpGetFileA" (ByVal hFtpSession As Integer, ByVal lpszRemoteFile As String, ByVal lpszNewFile As String, ByVal fFailIfExists As Boolean, ByVal dwFlagsAndAttributes As Integer, ByVal dwFlags As Integer, ByVal dwContext As Integer) As Boolean
    Private Declare Function FtpPutFile Lib "wininet.dll" Alias "FtpPutFileA" (ByVal hFtpSession As Integer, ByVal lpszLocalFile As String, ByVal lpszRemoteFile As String, ByVal dwFlags As Integer, ByVal dwContext As Integer) As Boolean


' example of how to use the FtpGetFile Function

     Sub Download()

       Dim INet, INetConn As Integer
       Dim RC As Boolean

       INet = InternetOpen("FTP Connection", 1, vbNullString, vbNullString, 0)
       INetConn = InternetConnect(INet, "www.myftpserver.com", 21, "myuser", "mypassword", 1, 0, 0)
       RC = FtpGetFile(INetConn, "FTPFolder/MyFiles/MyFile.txt", "C:\Temp\MyFile.txt", False, 1, 0, 0)
       If RC Then MsgBox("Transfer succesfull!")
       InternetCloseHandle(INetConn)
       InternetCloseHandle(INet)

   End Sub

' example of how to use the FtpPutFile Function

    Sub Upload()

        Dim INet, INetConn As Integer
        Dim RC As Boolean

        INet = InternetOpen("FTP Connection", 1, vbNullString, vbNullString, 0)
        INetConn = InternetConnect(INet, "www.watyf.com", 21, "myuser", "mypassword", 1, 0, 0)
        RC = FtpPutFile(INetConn, "C:\Temp\MyFile.txt", "FTPFolder/MyFiles/MyFile.txt", 0, 0)
        If RC Then MsgBox("Transfer succesfull!")
        InternetCloseHandle(INetConn)
        InternetCloseHandle(INet)

    End Sub

End Module
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.