Results 1 to 11 of 11

Thread: Access FTP Server

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2005
    Location
    Toronto, Canada
    Posts
    357

    Access FTP Server

    Hello people,
    Instead of creating resource files, what I want to do is that while my program is loading, I want my program to access a ftp server account and copy certain files on the ftp server account.

    I have been told that the proper way to do this is by using the INet control. I tried searching on the forums, but it was hard to find anything useful. I haven't found anything so far and I haven't used an INet control before neither heard of it. So I someone can find me a tutorial or atleast tell me how to access a certain ftp and copy the files, I would really appreciate it.

    Thank You all in advance

    Khanjan
    Hey... If you found this post helpful please rate it.

  2. #2
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Access FTP Server

    I would have your program write a batch file that sends the files using the ftp command built into windows. After it's written, your app could just shell it to send the files.

    Figure out how to send the files from a command prompt using the FTP command, and then write those commands into a file.

  3. #3
    Frenzied Member oh1mie's Avatar
    Join Date
    Sep 2001
    Location
    Finland
    Posts
    1,043

    Re: Access FTP Server

    Try this:

    VB Code:
    1. Option Explicit
    2.  
    3. Public Const INTERNET_OPEN_TYPE_DIRECT = 1                         ' direct to net
    4. Public Const INTERNET_OPEN_TYPE_PROXY = 3                          ' via named proxy
    5. Public Const INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY = 4    ' prevent using java/script/INS
    6. Public Const INTERNET_FLAG_PASSIVE = &H8000000             ' used for FTP connections
    7. Public Const INTERNET_FLAG_RELOAD = &H80000000
    8.  
    9. ' additional cache flags
    10. Public Const INTERNET_FLAG_NO_CACHE_WRITE = &H4000000      ' don't write this item to the cache
    11.  
    12. ' additional flags
    13. Public Const INTERNET_DEFAULT_FTP_PORT = 21                ' default for FTP servers
    14. Public Const INTERNET_FLAG_EXISTING_CONNECT = &H20000000   ' FTP: use existing InternetConnect handle for server if possible
    15. Public Const INTERNET_SERVICE_FTP = 1
    16.  
    17.  
    18. Public Declare Function InternetOpen Lib "Wininet.dll" _
    19.    Alias "InternetOpenA" _
    20.   (ByVal lpszAgent As String, _
    21.    ByVal dwAccessType As Long, _
    22.    ByVal lpszProxyName As String, _
    23.    ByVal lpszProxyBypass As String, _
    24.    ByVal dwFlags As Long) As Long
    25.  
    26. Public Declare Function InternetCloseHandle Lib "Wininet.dll" _
    27.    (ByVal hEnumHandle As Long) As Long
    28.  
    29. Public Declare Function InternetConnect Lib "Wininet.dll" _
    30.    Alias "InternetConnectA" _
    31.   (ByVal hInternet As Long, _
    32.    ByVal lpszServerName As String, _
    33.    ByVal nServerPort As Long, _
    34.    ByVal lpszUserName As String, _
    35.    ByVal lpszPassword As String, _
    36.    ByVal dwService As Long, _
    37.    ByVal dwFlags As Long, _
    38.    ByVal dwContext As Long) As Long
    39.  
    40. Public Declare Function FtpGetFile Lib "Wininet.dll" _
    41.    Alias "FtpGetFileA" _
    42.   (ByVal hConnect As Long, _
    43.    ByVal lpszRemoteFile As String, _
    44.    ByVal lpszNewFile As String, _
    45.    ByVal fFailIfExists As Long, _
    46.    ByVal dwFlagsAndAttributes As Long, _
    47.    ByVal dwFlags As Long, _
    48.    ByVal dwContext As Long) As Long
    49.  
    50. Private hInternet As Long
    51. Private hConnect  As Long
    52. Public Function LoadFromInternetWithFTP(ftpServerName As String, ftpUserName As String, ftpPassword As String _
    53. , strSourceFile As String, strTargetFile As String, strErrorMsg As String) As Boolean
    54.  
    55.    Screen.MousePointer = vbHourglass
    56.  
    57.    hInternet = InternetOpen("Vb FTP Transfer", INTERNET_OPEN_TYPE_DIRECT, _
    58.                              vbNullString, vbNullString, INTERNET_FLAG_NO_CACHE_WRITE)
    59.    
    60.       If hInternet Then
    61.          ' Try first passive
    62.          hConnect = InternetConnect(hInternet, ftpServerName, INTERNET_DEFAULT_FTP_PORT, ftpUserName, _
    63.                                  ftpPassword, INTERNET_SERVICE_FTP, INTERNET_FLAG_EXISTING_CONNECT Or _
    64.                                  INTERNET_FLAG_PASSIVE, &H0)
    65.          If hConnect = 0 Then
    66.             ' Then active connection
    67.             hConnect = InternetConnect(hInternet, ftpServerName, INTERNET_DEFAULT_FTP_PORT, ftpUserName, _
    68.                                  ftpPassword, INTERNET_SERVICE_FTP, INTERNET_FLAG_EXISTING_CONNECT Or _
    69.                                  INTERNET_FLAG_RELOAD, &H0)
    70.          End If
    71.  
    72.          If hConnect <> 0 Then
    73.             If FtpGetFile(hConnect, strSourceFile, strTargetFile, False, &H20, &H0, 0&) Then
    74.                LoadFromInternetWithFTP = True
    75.             End If
    76.          End If
    77.      
    78.       End If
    79.    
    80.    'Close FTP
    81.    Call InternetCloseHandle(hConnect)
    82.    Call InternetCloseHandle(hInternet)
    83.    
    84.    hInternet = 0
    85.    hConnect = 0
    86.  
    87.   Screen.MousePointer = vbDefault
    88.  
    89. End Function
    oh1mie/Vic


  4. #4
    New Member
    Join Date
    Feb 2006
    Posts
    3

    Re: Access FTP Server

    Try this:

    First select Internet Transfer Control from Project->Components
    Drag the control onto your form. Place a command button on your form. Then simply enter this code. Please enter your username & password correctly as given by the network administrator. Execute the code and check if your file has been uploaded to the server. You can then make necessary changes as you like.

    Private Sub Form_Load()
    Inet1.AccessType = icUseDefault
    Inet1.Protocol = icFTP
    End Sub

    Private Sub Command2_Click()

    With Inet1
    .URL = "ftp://yourwebsite.com"
    .UserName = "username"
    .Password = "password"
    .Execute , "PUT D:\yourfile_on_disk.txt /yourfile_on_server.txt"

    End With

  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Access FTP Server

    As others have supplied you with good working code, I'll give you a little tutorial.

    Using The INET Control

  6. #6
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Access FTP Server

    Attached is another tutorial I wrote a few years back and that used to be published at VB2theMax.com. It explains the API functions showed by oh1mie above together with a bunch of others FTP functions.
    Attached Files Attached Files

  7. #7
    Member
    Join Date
    Mar 2005
    Posts
    38

    Re: Access FTP Server

    Well I would suggest to use the very good cFTP class (by Andersson ?). Look for cFTP or FileCourier in the forum. Makes adding FTP function very easy. All you need to do is :

    OpenConnection
    GetFile or PutFile
    CloseConnection

    So Easy ! No need to go through the nitty grity of Inet.

    I have one problem though. How can I add a 'cancel' facility ? I want to let the user cancel a download and I think this class does not support cancel method. Any suggestion will be appreciated.

  8. #8
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Access FTP Server

    Quote Originally Posted by mecracked
    Well I would suggest to use the very good cFTP class (by Andersson ?). Look for cFTP or FileCourier in the forum. Makes adding FTP function very easy. All you need to do is :

    OpenConnection
    GetFile or PutFile
    CloseConnection

    So Easy ! No need to go through the nitty grity of Inet.

    I have one problem though. How can I add a 'cancel' facility ? I want to let the user cancel a download and I think this class does not support cancel method. Any suggestion will be appreciated.
    Glad you like the class . If you've found the FileCourier which I did publish as a reply to a thread here some time ago, you'll notice that it has an improved version of this class which indeed allows you to cancel the operation.

  9. #9
    Member
    Join Date
    Mar 2005
    Posts
    38

    Re: Access FTP Server

    Thanks Joacim !

    I'd been wishing that cancel operation for a long time. Seems I didn't look through the FileCourier code properly.

  10. #10
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Access FTP Server

    Well if you run FileCourier you'll notice that it shows a progress bar on upload/download with an estimated time it will take to finish the operation. This dialog also has a Cancel button.

  11. #11
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Access FTP Server

    BTW, Here is the thread in which I posted the source code for my File Courier program, if anyone is interested.

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