Results 1 to 4 of 4

Thread: FTP -Advice needed

  1. #1

    Thread Starter
    Fanatic Member Avatarp's Avatar
    Join Date
    Sep 2002
    Location
    Calgary
    Posts
    826

    FTP -Advice needed

    Hello and thank you for checking my post out. I have built a program that does many things. My program allows the user to manage their database and export data to an xml file. Now I need to FTP that xml file to a specific location on my server. Is there a fairly uncomplicated way of doing this?

  2. #2
    Addicted Member Optional's Avatar
    Join Date
    Jan 2010
    Location
    Rudimentary Space
    Posts
    214

    Re: FTP -Advice needed

    I through together some code based on information I was able to find on the internet.

    I'm not sure about it being "uncomplicated" but it seems quite streight forward ones all the declerations are out of the way.
    This does not mean though that there is not a pre-written VB6 component which does all that for you. I just don't know it if there is , someone else on this forum might point you to it, if there is one.

    I have no FTP server so I did not test the code below but it fully compiles and it does run.

    In my sample I use a single form with a command button, hard-coding basically everything I need when calling my method to upload the file
    It's up to you to replace those finctional values with the real file names or ftp server names, etc...

    I have never used VB6 to do any FTP upload but I'm sure the code below will either work with the correct values specified or at least be 80% complete for what you need to do.

    I declared the following in the general decleration section of my Form:
    VB Code:
    1. '/// <summary>
    2. '/// Forces explicit declaration of all variables in this file.
    3. '/// </summary>
    4. Option Explicit
    5.  
    6. '/// <summary>
    7. '/// Initializes an application's use of the Win32 Internet functions.
    8. '/// </summary>
    9. Private Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" (ByVal sAgent As String, ByVal lAccessType As Long, ByVal sProxyName As String, ByVal sProxyBypass As String, ByVal lFlags As Long) As Long
    10.  
    11. '/// <summary>
    12. '/// Opens an File Transfer Protocol (FTP) or HTTP session for a given site.
    13. '/// </summary>
    14. Private Declare Function InternetConnect Lib "wininet.dll" Alias "InternetConnectA" (ByVal hInternetSession As Long, ByVal sServerName As String, ByVal nServerPort As Integer, ByVal sUserName As String, ByVal sPassword As String, ByVal lService As Long, ByVal lFlags As Long, ByVal lContext As Long) As Long
    15.  
    16. '/// <summary>
    17. '/// Stores a file on the FTP server.
    18. '/// </summary>
    19. Private Declare Function FtpPutFile Lib "wininet.dll" Alias "FtpPutFileA" (ByVal hConnect As Long, ByVal lpszLocalFile As String, ByVal lpszNewRemoteFile As String, ByVal dwFlags As Long, ByVal dwContext As Long) As Boolean
    20.  
    21. '/// <summary>
    22. '/// Closes an Internet handle, as optained from InternetOpen or InternetConnect.
    23. '/// </summary>
    24. Private Declare Function InternetCloseHandle Lib "wininet" (ByVal hInet As Long) As Integer
    25.  
    26. '/// <summary>
    27. '/// Collection of valid Service Types.
    28. '/// </summary>
    29. Private Enum ServiceTypes
    30.     '/// <summary>Use FTP Service.</summary>
    31.     INTERNET_SERVICE_FTP = 1
    32.    
    33.     '/// <summary>Use FTP Service, using passive FTP semantics.</summary>
    34.     INTERNET_FLAG_PASSIVE = 134217728
    35.    
    36.     '/// <summary>Use Gopher Service.</summary>
    37.     INTERNET_SERVICE_GOPHER = 2
    38.    
    39.     '/// <summary>Use Http Service.</summary>
    40.     INTERNET_SERVICE_HTTP = 3
    41. End Enum
    42.  
    43. '/// <summary>
    44. '/// Collection of valid Port Types.
    45. '/// </summary>
    46. Private Enum PortTypes
    47.     '/// <summary>Use default port for FTP servers (port 21).</summary>
    48.     INTERNET_DEFAULT_FTP_PORT = 21
    49.    
    50.     '/// <summary>Use default port for Gopher servers (port 70).</summary>
    51.     INTERNET_DEFAULT_GOPHER_PORT = 70
    52.    
    53.     '/// <summary>Use default port for HTTP servers (port 80).</summary>
    54.     INTERNET_DEFAULT_HTTP_PORT = 80
    55.    
    56.     '/// <summary>Use default port for HTTPS servers (port 443).</summary>
    57.     INTERNET_DEFAULT_HTTPS_PORT = 443
    58.    
    59.     '/// <summary>Use default port for SOCKS firewall servers (port 1080).</summary>
    60.     INTERNET_DEFAULT_SOCKS_PORT = 1080
    61.    
    62.     '/// <summary>Use default port for the service specified by dwService.</summary>
    63.     INTERNET_INVALID_PORT_NUMBER = 0
    64. End Enum

    I did include all flags I could find for service and port values, even the ones you are not going to use. You might use them or not but you got the list now

    The I created an Upload method within the Form:
    VB Code:
    1. '/// <summary>
    2. '/// Uploads a file to an FTP Server.
    3. '/// </summary>
    4. '/// <param name="applicationName">The name of the application executing this method.</param>
    5. '/// <param name="server">The host-name of an FTP server.</param>
    6. '/// <param name="userName">The user name to use when connecting to the FTP Server.</param>
    7. '/// <param name="password">The password to use when connecting to the FTP Server.</param>
    8. '/// <param name="port">The TCP/IP port on the server to connect to.</param>
    9. '/// <param name="localFileName">The name of the file to be send from the local system.</param>
    10. '/// <param name="remoteFileName">The name of the file to be created on the remote system.</param>
    11. Public Sub UploadFileToFTP(applicationName As String, ByRef ftpServer As String, ByRef userName As String, ByRef password As String, ByRef port As Integer, ByRef localFileName As String, ByRef remoteFileName As String)
    12.     Dim session As Long
    13.     Dim connection As Long
    14.    
    15.     On Error GoTo Catch
    16.    
    17.     session = InternetOpen(applicationName, 0, vbNullString, vbNullString, 0)
    18.    
    19.     '-- This will log you into the default directory of the specified FTP Server.
    20.     '-- If you want to move to specific directories, use the FtpSetCurrentDirectory API (Not declared in this sample code).
    21.     connection = InternetConnect(session, ftpServer, port, userName, password, ServiceTypes.INTERNET_SERVICE_FTP, 0, 0)
    22.    
    23.     If FtpPutFile(connection, localFileName, remoteFileName, False, 1) = True Then
    24.         MsgBox localFileName & " uploaded successfully as " & remoteFileName & " to the specified server (" & ftpServer & ")"
    25.     Else
    26.         Err.Raise Err.LastDllError
    27.     End If
    28.    
    29. Finally:
    30.     Call InternetCloseHandle(connection)
    31.     Call InternetCloseHandle(session)
    32.     Exit Sub
    33.  
    34. Catch:
    35.     MsgBox "(" & Err.Number & ") " & Err.Description
    36.     Resume Finally
    37.  End Sub

    I'm only using the INTERNET_SERVICE_FTP to connect but if you need to connect using passive semantics, use INTERNET_FLAG_PASSIVE instead.

    To call the method I created a command button on the same form with the following code:
    VB Code:
    1. '/// <summary>
    2. '/// Occurs when uiUploadFTPFile has been clicked.
    3. '/// </summary>
    4. Private Sub uiUploadFTPFile_Click()
    5.     Call UploadFileToFTP("Testing FTP Upload", "[email protected]", "FTPUser", "MyPassword", PortTypes.INTERNET_DEFAULT_FTP_PORT, "MYLocalFile.doc", "MyRemoteFile.doc")
    6. End Sub

    As I said, I don't know or have any FTP Server I could test against, so I let you do that. Simply replace my default values with the true values you should have and off you go.

    I hope the example code above helps to give you a good starting point.
    Have Fun, I know I did

    Resources on the APIs I used
    Last edited by Optional; Mar 9th, 2010 at 07:34 AM.



    Kind Regards,
    Optional



    If you feel this post has helped in answering your question please return the favour and Rate this post.
    If your problem has been solved and your question has been answered mark the thread as [RESOLVED] by selecting the Thread Tools menu option at the top and clicking the Mark Thread Resolved menu item.


    VB6 - (DataGrid) Get the Row selected with the right mouse button



  3. #3

    Thread Starter
    Fanatic Member Avatarp's Avatar
    Join Date
    Sep 2002
    Location
    Calgary
    Posts
    826

    Re: FTP -Advice needed

    Hello Optional I thank you very much for your fantastic post. I will try this coding out in the next day or two when I have my computer back from work. I'll reply when I have tested it.

    Thanks again!

  4. #4
    New Member
    Join Date
    Feb 2010
    Location
    iraq
    Posts
    12

    Re: FTP -Advice needed

    Quote Originally Posted by Optional View Post
    I through together some code based on information I was able to find on the internet.

    I'm not sure about it being "uncomplicated" but it seems quite streight forward ones all the declerations are out of the way.
    This does not mean though that there is not a pre-written VB6 component which does all that for you. I just don't know it if there is , someone else on this forum might point you to it, if there is one.

    I have no FTP server so I did not test the code below but it fully compiles and it does run.

    In my sample I use a single form with a command button, hard-coding basically everything I need when calling my method to upload the file
    It's up to you to replace those finctional values with the real file names or ftp server names, etc...

    I have never used VB6 to do any FTP upload but I'm sure the code below will either work with the correct values specified or at least be 80% complete for what you need to do.

    I declared the following in the general decleration section of my Form:
    VB Code:
    1. '/// <summary>
    2. '/// Forces explicit declaration of all variables in this file.
    3. '/// </summary>
    4. Option Explicit
    5.  
    6. '/// <summary>
    7. '/// Initializes an application's use of the Win32 Internet functions.
    8. '/// </summary>
    9. Private Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" (ByVal sAgent As String, ByVal lAccessType As Long, ByVal sProxyName As String, ByVal sProxyBypass As String, ByVal lFlags As Long) As Long
    10.  
    11. '/// <summary>
    12. '/// Opens an File Transfer Protocol (FTP) or HTTP session for a given site.
    13. '/// </summary>
    14. Private Declare Function InternetConnect Lib "wininet.dll" Alias "InternetConnectA" (ByVal hInternetSession As Long, ByVal sServerName As String, ByVal nServerPort As Integer, ByVal sUserName As String, ByVal sPassword As String, ByVal lService As Long, ByVal lFlags As Long, ByVal lContext As Long) As Long
    15.  
    16. '/// <summary>
    17. '/// Stores a file on the FTP server.
    18. '/// </summary>
    19. Private Declare Function FtpPutFile Lib "wininet.dll" Alias "FtpPutFileA" (ByVal hConnect As Long, ByVal lpszLocalFile As String, ByVal lpszNewRemoteFile As String, ByVal dwFlags As Long, ByVal dwContext As Long) As Boolean
    20.  
    21. '/// <summary>
    22. '/// Closes an Internet handle, as optained from InternetOpen or InternetConnect.
    23. '/// </summary>
    24. Private Declare Function InternetCloseHandle Lib "wininet" (ByVal hInet As Long) As Integer
    25.  
    26. '/// <summary>
    27. '/// Collection of valid Service Types.
    28. '/// </summary>
    29. Private Enum ServiceTypes
    30.     '/// <summary>Use FTP Service.</summary>
    31.     INTERNET_SERVICE_FTP = 1
    32.    
    33.     '/// <summary>Use FTP Service, using passive FTP semantics.</summary>
    34.     INTERNET_FLAG_PASSIVE = 134217728
    35.    
    36.     '/// <summary>Use Gopher Service.</summary>
    37.     INTERNET_SERVICE_GOPHER = 2
    38.    
    39.     '/// <summary>Use Http Service.</summary>
    40.     INTERNET_SERVICE_HTTP = 3
    41. End Enum
    42.  
    43. '/// <summary>
    44. '/// Collection of valid Port Types.
    45. '/// </summary>
    46. Private Enum PortTypes
    47.     '/// <summary>Use default port for FTP servers (port 21).</summary>
    48.     INTERNET_DEFAULT_FTP_PORT = 21
    49.    
    50.     '/// <summary>Use default port for Gopher servers (port 70).</summary>
    51.     INTERNET_DEFAULT_GOPHER_PORT = 70
    52.    
    53.     '/// <summary>Use default port for HTTP servers (port 80).</summary>
    54.     INTERNET_DEFAULT_HTTP_PORT = 80
    55.    
    56.     '/// <summary>Use default port for HTTPS servers (port 443).</summary>
    57.     INTERNET_DEFAULT_HTTPS_PORT = 443
    58.    
    59.     '/// <summary>Use default port for SOCKS firewall servers (port 1080).</summary>
    60.     INTERNET_DEFAULT_SOCKS_PORT = 1080
    61.    
    62.     '/// <summary>Use default port for the service specified by dwService.</summary>
    63.     INTERNET_INVALID_PORT_NUMBER = 0
    64. End Enum

    I did include all flags I could find for service and port values, even the ones you are not going to use. You might use them or not but you got the list now

    The I created an Upload method within the Form:
    VB Code:
    1. '/// <summary>
    2. '/// Uploads a file to an FTP Server.
    3. '/// </summary>
    4. '/// <param name="applicationName">The name of the application executing this method.</param>
    5. '/// <param name="server">The host-name of an FTP server.</param>
    6. '/// <param name="userName">The user name to use when connecting to the FTP Server.</param>
    7. '/// <param name="password">The password to use when connecting to the FTP Server.</param>
    8. '/// <param name="port">The TCP/IP port on the server to connect to.</param>
    9. '/// <param name="localFileName">The name of the file to be send from the local system.</param>
    10. '/// <param name="remoteFileName">The name of the file to be created on the remote system.</param>
    11. Public Sub UploadFileToFTP(applicationName As String, ByRef ftpServer As String, ByRef userName As String, ByRef password As String, ByRef port As Integer, ByRef localFileName As String, ByRef remoteFileName As String)
    12.     Dim session As Long
    13.     Dim connection As Long
    14.    
    15.     On Error GoTo Catch
    16.    
    17.     session = InternetOpen(applicationName, 0, vbNullString, vbNullString, 0)
    18.    
    19.     '-- This will log you into the default directory of the specified FTP Server.
    20.     '-- If you want to move to specific directories, use the FtpSetCurrentDirectory API (Not declared in this sample code).
    21.     connection = InternetConnect(session, ftpServer, port, userName, password, ServiceTypes.INTERNET_SERVICE_FTP, 0, 0)
    22.    
    23.     If FtpPutFile(connection, localFileName, remoteFileName, False, 1) = True Then
    24.         MsgBox localFileName & " uploaded successfully as " & remoteFileName & " to the specified server (" & ftpServer & ")"
    25.     Else
    26.         Err.Raise Err.LastDllError
    27.     End If
    28.    
    29. Finally:
    30.     Call InternetCloseHandle(connection)
    31.     Call InternetCloseHandle(session)
    32.     Exit Sub
    33.  
    34. Catch:
    35.     MsgBox "(" & Err.Number & ") " & Err.Description
    36.     Resume Finally
    37.  End Sub

    I'm only using the INTERNET_SERVICE_FTP to connect but if you need to connect using passive semantics, use INTERNET_FLAG_PASSIVE instead.

    To call the method I created a command button on the same form with the following code:
    VB Code:
    1. '/// <summary>
    2. '/// Occurs when uiUploadFTPFile has been clicked.
    3. '/// </summary>
    4. Private Sub uiUploadFTPFile_Click()
    5.     Call UploadFileToFTP("Testing FTP Upload", "[email protected]", "FTPUser", "MyPassword", PortTypes.INTERNET_DEFAULT_FTP_PORT, "MYLocalFile.doc", "MyRemoteFile.doc")
    6. End Sub

    As I said, I don't know or have any FTP Server I could test against, so I let you do that. Simply replace my default values with the true values you should have and off you go.

    I hope the example code above helps to give you a good starting point.
    Have Fun, I know I did

    Resources on the APIs I used
    Thank you,

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width