Visual Basic Internet Routines
FtpGetFile: Download Files via FTP
     
Posted:   Saturday January 08, 2000
Updated:   Monday December 26, 2011
     
Applies to:   VB4-32, VB5, VB6
Developed with:   VB6, Windows NT4
OS restrictions:   None
Author:   VBnet - Randy Birch
     

Related:  

DoFileDownload: Customize the IE Download Dialog
DoFileDownload: Download Files Using IE's Download Dialog
FtpFindFirstFile: Connect and Retrieve FTP File Listings

FtpFindFirstFile: Download Files via FTP with a Download Progress Callback
     
 Prerequisites
Established network connection or personal web server/IIS and the project created in FtpFindFirstFile: Connect and Retrieve FTP File Listings.

Adding a simple file download mechanism to the demo created in FtpFindFirstFile: Connect and Retrieve FTP File Listings is straightforward, requiring only one API, and code added to the Command4_Click event, as well as the List1_Click event.
 BAS Module Code
Add the following additional code to the FtpFindFirstFile: Connect and Retrieve FTP File Listings BAS module:

Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright 1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Declare Function FtpGetFile Lib "wininet" _
   Alias "FtpGetFileA" _
  (ByVal hConnect As Long, _
   ByVal lpszRemoteFile As String, _
   ByVal lpszNewFile As String, _
   ByVal fFailIfExists As Long, _
   ByVal dwFlagsAndAttributes As Long, _
   ByVal dwFlags As Long, _
   ByVal dwContext As Long) As Long
 Form Code
To the form code from FtpFindFirstFile: Connect and Retrieve FTP File Listings, change the Form_Load code to set the caption of Command4 to "Download", and add the following additional code to the form:

Option Explicit
Private Sub List1_Click()

   Dim sNewPath As String
   Dim sPath As String
   
  'get the desired directory from the list
   sNewPath = List1.List(List1.ListIndex)
   
  'If a root item or directory is selected,
  'disable the download button
   Command4.Enabled = (Right$(sNewPath, 1) <> sSlash) And _
                      (sNewPath <> sRootDots)

End Sub


Private Sub Command4_Click()
   
   Dim sRemoteFile As String
   Dim sNewFile As String
   Dim sFile As String
   Dim sCurrDir As String
    
  'Show the wait cursor
   Screen.MousePointer = vbHourglass
   
  'Only if a valid connection...
   If hConnect Then
      
     'get the current directory and
     'selected list item
      sCurrDir = GetFTPDirectory(hConnect)
      sFile = (List1.List(List1.ListIndex))

     'build the necessary strings. The
     'directory is qualified, so contains
     'the terminating slash. 
     '
     'Change sNewFile to a valid path 
     'on your system!
      sRemoteFile = sCurrDir & sFile
      sNewFile = "d:\ftptest\" & sFile
      
     'just a message to tell what's happening
      Text2.Text = "attempting to downloaded " & sFile & sRootDots
      Text2.Refresh
      
     'download the file
      If FtpGetFile(hConnect, _
                    sRemoteFile, _
                    sNewFile, _
                    False, _
                    FILE_ATTRIBUTE_ARCHIVE, _
                    FTP_TRANSFER_TYPE_UNKNOWN, _
                    0&) Then
         
        'show the success message
         Text2.Text = sFile & " has been downloaded to " & sNewFile
                       
      Else
      
        'show any error
         Text2.Text = GetErr(Err.LastDllError)
      
      End If  'If FtpGetFile
   End If  'If hConnect

   Screen.MousePointer = vbDefault
   
End Sub
 Comments
Now, selecting a file will enable the download button which coincidentally, when pressed, will do just that. Make sure that the target directory on your system exists.

As coded, the download method will overwrite existing files of the same name. To prevent this, set the fFailIfExists flag in the FtpGetFile API to 1.


 
 

PayPal Link
Make payments with PayPal - it's fast, free and secure!

 
 
 
 

Copyright 1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy

 

Hit Counter