[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Use FTP from asp .net

Posted on 2004-10-26
7
Medium Priority
?
379 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
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 video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

656 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