|
-
Mar 9th, 2010, 05:25 AM
#1
Thread Starter
Fanatic Member
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?
-
Mar 9th, 2010, 07:24 AM
#2
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:
'/// <summary> '/// Forces explicit declaration of all variables in this file. '/// </summary> Option Explicit '/// <summary> '/// Initializes an application's use of the Win32 Internet functions. '/// </summary> 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 '/// <summary> '/// Opens an File Transfer Protocol (FTP) or HTTP session for a given site. '/// </summary> 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 '/// <summary> '/// Stores a file on the FTP server. '/// </summary> 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 '/// <summary> '/// Closes an Internet handle, as optained from InternetOpen or InternetConnect. '/// </summary> Private Declare Function InternetCloseHandle Lib "wininet" (ByVal hInet As Long) As Integer '/// <summary> '/// Collection of valid Service Types. '/// </summary> Private Enum ServiceTypes '/// <summary>Use FTP Service.</summary> INTERNET_SERVICE_FTP = 1 '/// <summary>Use FTP Service, using passive FTP semantics.</summary> INTERNET_FLAG_PASSIVE = 134217728 '/// <summary>Use Gopher Service.</summary> INTERNET_SERVICE_GOPHER = 2 '/// <summary>Use Http Service.</summary> INTERNET_SERVICE_HTTP = 3 End Enum '/// <summary> '/// Collection of valid Port Types. '/// </summary> Private Enum PortTypes '/// <summary>Use default port for FTP servers (port 21).</summary> INTERNET_DEFAULT_FTP_PORT = 21 '/// <summary>Use default port for Gopher servers (port 70).</summary> INTERNET_DEFAULT_GOPHER_PORT = 70 '/// <summary>Use default port for HTTP servers (port 80).</summary> INTERNET_DEFAULT_HTTP_PORT = 80 '/// <summary>Use default port for HTTPS servers (port 443).</summary> INTERNET_DEFAULT_HTTPS_PORT = 443 '/// <summary>Use default port for SOCKS firewall servers (port 1080).</summary> INTERNET_DEFAULT_SOCKS_PORT = 1080 '/// <summary>Use default port for the service specified by dwService.</summary> INTERNET_INVALID_PORT_NUMBER = 0 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:
'/// <summary> '/// Uploads a file to an FTP Server. '/// </summary> '/// <param name="applicationName">The name of the application executing this method.</param> '/// <param name="server">The host-name of an FTP server.</param> '/// <param name="userName">The user name to use when connecting to the FTP Server.</param> '/// <param name="password">The password to use when connecting to the FTP Server.</param> '/// <param name="port">The TCP/IP port on the server to connect to.</param> '/// <param name="localFileName">The name of the file to be send from the local system.</param> '/// <param name="remoteFileName">The name of the file to be created on the remote system.</param> 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) Dim session As Long Dim connection As Long On Error GoTo Catch session = InternetOpen(applicationName, 0, vbNullString, vbNullString, 0) '-- This will log you into the default directory of the specified FTP Server. '-- If you want to move to specific directories, use the FtpSetCurrentDirectory API (Not declared in this sample code). connection = InternetConnect(session, ftpServer, port, userName, password, ServiceTypes.INTERNET_SERVICE_FTP, 0, 0) If FtpPutFile(connection, localFileName, remoteFileName, False, 1) = True Then MsgBox localFileName & " uploaded successfully as " & remoteFileName & " to the specified server (" & ftpServer & ")" Else Err.Raise Err.LastDllError End If Finally: Call InternetCloseHandle(connection) Call InternetCloseHandle(session) Exit Sub Catch: MsgBox "(" & Err.Number & ") " & Err.Description Resume Finally 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:
'/// <summary> '/// Occurs when uiUploadFTPFile has been clicked. '/// </summary> Private Sub uiUploadFTPFile_Click() Call UploadFileToFTP("Testing FTP Upload", " [email protected]", "FTPUser", "MyPassword", PortTypes.INTERNET_DEFAULT_FTP_PORT, "MYLocalFile.doc", "MyRemoteFile.doc") 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
-
Mar 9th, 2010, 09:40 AM
#3
Thread Starter
Fanatic Member
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!
-
May 19th, 2010, 07:52 PM
#4
New Member
Re: FTP -Advice needed
 Originally Posted by Optional
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:
'/// <summary>
'/// Forces explicit declaration of all variables in this file.
'/// </summary>
Option Explicit
'/// <summary>
'/// Initializes an application's use of the Win32 Internet functions.
'/// </summary>
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
'/// <summary>
'/// Opens an File Transfer Protocol (FTP) or HTTP session for a given site.
'/// </summary>
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
'/// <summary>
'/// Stores a file on the FTP server.
'/// </summary>
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
'/// <summary>
'/// Closes an Internet handle, as optained from InternetOpen or InternetConnect.
'/// </summary>
Private Declare Function InternetCloseHandle Lib "wininet" (ByVal hInet As Long) As Integer
'/// <summary>
'/// Collection of valid Service Types.
'/// </summary>
Private Enum ServiceTypes
'/// <summary>Use FTP Service.</summary>
INTERNET_SERVICE_FTP = 1
'/// <summary>Use FTP Service, using passive FTP semantics.</summary>
INTERNET_FLAG_PASSIVE = 134217728
'/// <summary>Use Gopher Service.</summary>
INTERNET_SERVICE_GOPHER = 2
'/// <summary>Use Http Service.</summary>
INTERNET_SERVICE_HTTP = 3
End Enum
'/// <summary>
'/// Collection of valid Port Types.
'/// </summary>
Private Enum PortTypes
'/// <summary>Use default port for FTP servers (port 21).</summary>
INTERNET_DEFAULT_FTP_PORT = 21
'/// <summary>Use default port for Gopher servers (port 70).</summary>
INTERNET_DEFAULT_GOPHER_PORT = 70
'/// <summary>Use default port for HTTP servers (port 80).</summary>
INTERNET_DEFAULT_HTTP_PORT = 80
'/// <summary>Use default port for HTTPS servers (port 443).</summary>
INTERNET_DEFAULT_HTTPS_PORT = 443
'/// <summary>Use default port for SOCKS firewall servers (port 1080).</summary>
INTERNET_DEFAULT_SOCKS_PORT = 1080
'/// <summary>Use default port for the service specified by dwService.</summary>
INTERNET_INVALID_PORT_NUMBER = 0
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:
'/// <summary>
'/// Uploads a file to an FTP Server.
'/// </summary>
'/// <param name="applicationName">The name of the application executing this method.</param>
'/// <param name="server">The host-name of an FTP server.</param>
'/// <param name="userName">The user name to use when connecting to the FTP Server.</param>
'/// <param name="password">The password to use when connecting to the FTP Server.</param>
'/// <param name="port">The TCP/IP port on the server to connect to.</param>
'/// <param name="localFileName">The name of the file to be send from the local system.</param>
'/// <param name="remoteFileName">The name of the file to be created on the remote system.</param>
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)
Dim session As Long
Dim connection As Long
On Error GoTo Catch
session = InternetOpen(applicationName, 0, vbNullString, vbNullString, 0)
'-- This will log you into the default directory of the specified FTP Server.
'-- If you want to move to specific directories, use the FtpSetCurrentDirectory API (Not declared in this sample code).
connection = InternetConnect(session, ftpServer, port, userName, password, ServiceTypes.INTERNET_SERVICE_FTP, 0, 0)
If FtpPutFile(connection, localFileName, remoteFileName, False, 1) = True Then
MsgBox localFileName & " uploaded successfully as " & remoteFileName & " to the specified server (" & ftpServer & ")"
Else
Err.Raise Err.LastDllError
End If
Finally:
Call InternetCloseHandle(connection)
Call InternetCloseHandle(session)
Exit Sub
Catch:
MsgBox "(" & Err.Number & ") " & Err.Description
Resume Finally
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:
'/// <summary>
'/// Occurs when uiUploadFTPFile has been clicked.
'/// </summary>
Private Sub uiUploadFTPFile_Click()
Call UploadFileToFTP("Testing FTP Upload", " [email protected]", "FTPUser", "MyPassword", PortTypes.INTERNET_DEFAULT_FTP_PORT, "MYLocalFile.doc", "MyRemoteFile.doc")
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|