Results 1 to 4 of 4

Thread: Automatically downloading file from internet

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2001
    Posts
    95

    Automatically downloading file from internet

    Hi there,
    I need to have a program that will automatically download a file from the Internet. I need it for a Mcafee virus update and the URL would be
    http://download.nai.com/products/dat...i/dat-4214.zip
    I know that dat-4214.zip will change but I think I should be able to handle that.
    I don't know where to start. APIs???? Controls?????
    Any ideas anybody?
    Thanks
    Ray

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Jul 2001
    Posts
    95
    Found the answer in a seperate post.

    Code:
    Private Declare Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" (ByVal pCaller As Long, ByVal szURL As String, ByVal szFileName As String, ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long
    Public Function DownloadFile(URL As String, LocalFilename As String) As Boolean
        Dim lngRetVal As Long
        lngRetVal = URLDownloadToFile(0, URL, LocalFilename, 0, 0)
        If lngRetVal = 0 Then DownloadFile = True
    End Function
    Private Sub Form_Load()
        'example by Matthew Gates ([email protected])
    DownloadFile "http://download.nai.com/products/datfiles/4.x/nai/dat-4214.zip", "c:\setupray.exe"
    
    End Sub

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    These FTP API Examples should help...
    VB Code:
    1. Private Const FTP_TRANSFER_TYPE_UNKNOWN = &H0
    2. Private Const FTP_TRANSFER_TYPE_ASCII = &H1
    3. Private Const FTP_TRANSFER_TYPE_BINARY = &H2
    4. Private Const INTERNET_DEFAULT_FTP_PORT = 21                ' default for FTP servers
    5. Private Const INTERNET_SERVICE_FTP = 1
    6. Private Const INTERNET_FLAG_PASSIVE = &H8000000             ' used for FTP connections
    7. Private Const INTERNET_OPEN_TYPE_PRECONFIG = 0                     ' use registry configuration
    8. Private Const INTERNET_OPEN_TYPE_DIRECT = 1                         ' direct to net
    9. Private Const INTERNET_OPEN_TYPE_PROXY = 3                          ' via named proxy
    10. Private Const INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY = 4    ' prevent using java/script/INS
    11. Private Const MAX_PATH = 260
    12. Private Const PassiveConnection As Boolean = True
    13.  
    14. Private Type FILETIME
    15.     dwLowDateTime As Long
    16.     dwHighDateTime As Long
    17. End Type
    18.  
    19. Private Type WIN32_FIND_DATA
    20.     dwFileAttributes As Long
    21.     ftCreationTime As FILETIME
    22.     ftLastAccessTime As FILETIME
    23.     ftLastWriteTime As FILETIME
    24.     nFileSizeHigh As Long
    25.     nFileSizeLow As Long
    26.     dwReserved0 As Long
    27.     dwReserved1 As Long
    28.     cFileName As String * MAX_PATH
    29.     cAlternate As String * 14
    30. End Type
    31.  
    32. Private Declare Function InternetCloseHandle Lib "wininet.dll" (ByVal hInet As Long) As Integer
    33. 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
    34. 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
    35. Private Declare Function FtpSetCurrentDirectory Lib "wininet.dll" Alias "FtpSetCurrentDirectoryA" (ByVal hFtpSession As Long, ByVal lpszDirectory As String) As Boolean
    36. Private Declare Function FtpGetCurrentDirectory Lib "wininet.dll" Alias "FtpGetCurrentDirectoryA" (ByVal hFtpSession As Long, ByVal lpszCurrentDirectory As String, lpdwCurrentDirectory As Long) As Long
    37. Private Declare Function FtpCreateDirectory Lib "wininet.dll" Alias "FtpCreateDirectoryA" (ByVal hFtpSession As Long, ByVal lpszDirectory As String) As Boolean
    38. Private Declare Function FtpRemoveDirectory Lib "wininet.dll" Alias "FtpRemoveDirectoryA" (ByVal hFtpSession As Long, ByVal lpszDirectory As String) As Boolean
    39. Private Declare Function FtpDeleteFile Lib "wininet.dll" Alias "FtpDeleteFileA" (ByVal hFtpSession As Long, ByVal lpszFileName As String) As Boolean
    40. Private Declare Function FtpRenameFile Lib "wininet.dll" Alias "FtpRenameFileA" (ByVal hFtpSession As Long, ByVal lpszExisting As String, ByVal lpszNew As String) As Boolean
    41. Private Declare Function FtpGetFile Lib "wininet.dll" 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, ByRef dwContext As Long) As Boolean
    42. 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
    43. Private Declare Function InternetGetLastResponseInfo Lib "wininet.dll" Alias "InternetGetLastResponseInfoA" (lpdwError As Long, ByVal lpszBuffer As String, lpdwBufferLength As Long) As Boolean
    44. Private Declare Function FtpFindFirstFile Lib "wininet.dll" Alias "FtpFindFirstFileA" (ByVal hFtpSession As Long, ByVal lpszSearchFile As String, lpFindFileData As WIN32_FIND_DATA, ByVal dwFlags As Long, ByVal dwContent As Long) As Long
    45. Private Declare Function InternetFindNextFile Lib "wininet.dll" Alias "InternetFindNextFileA" (ByVal hFind As Long, lpvFindData As WIN32_FIND_DATA) As Long
    46.  
    47.  
    48. Private Sub EnumFiles(hConnection As Long)
    49.     Dim pData As WIN32_FIND_DATA, hFind As Long, lRet As Long
    50.     'set the graphics mode to persistent
    51.     Me.AutoRedraw = True
    52.     'create a buffer
    53.     pData.cFileName = String(MAX_PATH, 0)
    54.     'find the first file
    55.     hFind = FtpFindFirstFile(hConnection, "*.*", pData, 0, 0)
    56.     'if there's no file, then exit sub
    57.     If hFind = 0 Then Exit Sub
    58.     'show the filename
    59.     Me.Print Left(pData.cFileName, InStr(1, pData.cFileName, String(1, 0), vbBinaryCompare) - 1)
    60.     Do
    61.         'create a buffer
    62.         pData.cFileName = String(MAX_PATH, 0)
    63.         'find the next file
    64.         lRet = InternetFindNextFile(hFind, pData)
    65.         'if there's no next file, exit do
    66.         If lRet = 0 Then Exit Do
    67.         'show the filename
    68.         Me.Print Left(pData.cFileName, InStr(1, pData.cFileName, String(1, 0), vbBinaryCompare) - 1)
    69.     Loop
    70.     'close the search handle
    71.     InternetCloseHandle hFind
    72. End Sub
    73.  
    74. Private Sub ShowError()
    75.     Dim lErr As Long, sErr As String, lenBuf As Long
    76.     'get the required buffer size
    77.     InternetGetLastResponseInfo lErr, sErr, lenBuf
    78.     'create a buffer
    79.     sErr = String(lenBuf, 0)
    80.     'retrieve the last respons info
    81.     InternetGetLastResponseInfo lErr, sErr, lenBuf
    82.     'show the last response info
    83.     MsgBox "Error " + CStr(lErr) + ": " + sErr, vbOKOnly + vbCritical
    84. End Sub
    85.  
    86. Private Sub Command1_Click()
    87.     'KPD-Team 2000
    88.     'URL: [url]http://www.allapi.net[/url]
    89.     'E-Mail: [email][email protected][/email]
    90.     Dim hConnection As Long, hOpen As Long, sOrgPath  As String
    91.     'open an internet connection
    92.     hOpen = InternetOpen("API-Guide sample program", INTERNET_OPEN_TYPE_PRECONFIG, vbNullString, vbNullString, 0)
    93.     'connect to the FTP server
    94.     hConnection = InternetConnect(hOpen, "your ftp server", INTERNET_DEFAULT_FTP_PORT, "your login", "your password", INTERNET_SERVICE_FTP, IIf(PassiveConnection, INTERNET_FLAG_PASSIVE, 0), 0)
    95.     'create a buffer to store the original directory
    96.     sOrgPath = String(MAX_PATH, 0)
    97.     'get the directory
    98.     FtpGetCurrentDirectory hConnection, sOrgPath, Len(sOrgPath)
    99.     'create a new directory 'testing'
    100.     FtpCreateDirectory hConnection, "testing"
    101.     'set the current directory to 'root/testing'
    102.     FtpSetCurrentDirectory hConnection, "testing"
    103.     'upload the file 'test.htm'
    104.     FtpPutFile hConnection, "C:\test.htm", "test.htm", FTP_TRANSFER_TYPE_UNKNOWN, 0
    105.     'rename 'test.htm' to 'apiguide.htm'
    106.     FtpRenameFile hConnection, "test.htm", "apiguide.htm"
    107.     'enumerate the file list from the current directory ('root/testing')
    108.     EnumFiles hConnection
    109.     'retrieve the file from the FTP server
    110.     FtpGetFile hConnection, "apiguide.htm", "c:\apiguide.htm", False, 0, FTP_TRANSFER_TYPE_UNKNOWN, 0
    111.     'delete the file from the FTP server
    112.     FtpDeleteFile hConnection, "apiguide.htm"
    113.     'set the current directory back to the root
    114.     FtpSetCurrentDirectory hConnection, sOrgPath
    115.     'remove the direcrtory 'testing'
    116.     FtpRemoveDirectory hConnection, "testing"
    117.     'close the FTP connection
    118.     InternetCloseHandle hConnection
    119.     'close the internet connection
    120.     InternetCloseHandle hOpen
    121. End Sub

  4. #4
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989
    How will the FTP api help when the file is on HTTP??

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