Solved

Use FTP from asp .net

Posted on 2004-10-26
357 Views
Last Modified: 2009-07-29
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
0
Question by:dmontgom
    7 Comments
     
    LVL 17

    Accepted Solution

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

    source code available for download
    0
     
    LVL 17

    Expert Comment

    by:AerosSaga
    0
     
    LVL 17

    Expert Comment

    by:AerosSaga
    0
     
    LVL 17

    Expert Comment

    by:AerosSaga
    0
     
    LVL 17

    Expert Comment

    by:AerosSaga
    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
     
    LVL 17

    Expert Comment

    by:AerosSaga
    0
     
    LVL 17

    Expert Comment

    by:AerosSaga
    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
    In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
    In this Experts Exchange video Micro Tutorial, I'm going to show how small business owners who use Google Apps can save money by setting up what is called a catch-all email address in their Gmail accounts. By using the catch-all feature, small busin…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    845 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    5 Experts available now in Live!

    Get 1:1 Help Now