?
Solved

Use FTP from asp .net

Posted on 2004-10-26
7
Medium Priority
?
387 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
Comment
Question by:dmontgom
  • 7
7 Comments
 
LVL 17

Accepted Solution

by:
AerosSaga earned 500 total points
ID: 12411720
http://www.freevbcode.com/ShowCode.asp?ID=4655

source code available for download
0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 12411794
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
LVL 17

Expert Comment

by:AerosSaga
ID: 12411796
0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 12411803
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
ID: 12411823
0
 
LVL 17

Expert Comment

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

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
How to fix display issue, screen flickering issue when I plug in power cord to the machine. Before I start explaining the solution lets check out once the issue how it looks like after I connect the power cord. most of you also have faced this…
Free Data Recovery software is an advanced solution from Kernel Tools to recover data and files such as documents, emails, database, media and pictures, etc. It supports recovery from physical & logical drive after a hard disk crash, accidental/inte…

593 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