Use VB to implement FTP upload and download

Source: Internet
Author: User
Tags filetime

Option explicit

Public 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

Public 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

Public declare function ftpgetfile lib "wininet. dll" alias "ftpgetfilea "_
(Byval hftpsession as long, byval lpszremotefile as string ,_
Byval lpsznewfile as string, byval ffailifexists as Boolean ,_
Byval dwflagsandattributes as long, byval dwflags as long ,_
Byval dwcontext as long) as Boolean

Public declare function ftpputfile lib "wininet. dll" alias "ftpputfilea "_
(Byval hftpsession as long, byval lpszlocalfile as string ,_
Byval lpszremotefile as string, byval dwflags as long ,_
Byval dwcontext as long) as Boolean

Public declare function ftpdeletefile lib "wininet. dll" alias "ftpdeletefilea "_
(Byval hftpsession as long, byval lpszfilename as string) as Boolean

Public declare function ftprenamefile lib "wininet. dll" alias "ftprenamefilea "_
(Byval hftpsession as long, byval lpszexsiting as string, byval lpsznew as string) as Boolean

Public declare function internetclosehandle lib "wininet. dll" (byval hinet as long) as integer

Public 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

Public declare function internetfindnextfile lib "wininet. dll" alias "internetfindnextfilea "_
(Byval hfind as long, lpvfnddata as win32_find_data) as long

Const internet_open_type_preconfig = 0

Public type filetime
Dwlowdatetime as long
Dwhighdatetime as long
End type
Public type win32_find_data
Dwfilattributes as long
Ftcreationtime as filetime
Ftlastaccesstime as filetime
Ftlastwritetime as filetime
Nfilesizehigh as long
Nfilesizelow as long
Dwreserved0 as long
Dwreserved1 as long
Cfilename as string * 260
Calternate as string * 14
End type

 
Public declare function getopenfilename lib "comdlg32.dll" alias _
"Getopenfilenamea" (popenfilename as openfilename) as long
 
Type openfilename
Lstructsize as long
Hwndowner as long
Hinstance as long
Lpstrfilter as string
Lpstrcustomfilter as string
Nmaxcustfilter as long
Nfilterindex as long
Lpstrfile as string
Nmaxfile as long
Lpstrfiletitle as string
Nmaxfiletitle as long
Lpstrinitialdir as string
Lpstrtitle as string
Flags as long
Nfileoffset as integer
Nfileextension as integer
Lpstrdefext as string
Lcustdata as long
Lpfnhook as long
Lptemplatename as string
End type
Private sub commandementclick ()
Lnginet = internetopen (vbnullstring, internet_open_type_preconfig ,_
Vbnullstring, vbnullstring, 0 &)
If lnginet then
Lnginetconn = internetconnect (lnginet, "ip address", 0 ,_
"Username", "password", 1, 0, 0)
If lnginetconn then
Blnrc = ftpgetfile (lnginetconn, "website/address. asp", "C:/address. asp", 0, 0, 1, 0)
If blnrc then
Msgbox "Download OK !!! "
End if
Internetclosehandle lnginetconn
Internetclosehandle lnginet
Msgbox "close OK !!! "
Else
Msgbox "can't connect"
End if
Else
Msgbox "FTP wrong"
End if
End sub

Private sub command2_click ()
Dim pdata as win32_find_data
Dim lnghinet as long
Dim interror as integer
Dim strtemp as string
Dim blnrc as Boolean
Pdata. cfilename = string (260, 0)
Lnginet = internetopen (vbnullstring, internet_open_type_preconfig ,_
Vbnullstring, vbnullstring, 0 &)
If lnginet then
Lnginetconn = internetconnect (lnginet, "ip address", 0 ,_
"Username", "password", 1, 0, 0)
If lnginetconn then
Lnghinet = ftpfindfirstfile (lnginetconn, "*. *", pdata, 0, 0)
Strtemp = left (pdata. cfilename, instr (1, pdata. cfilename, string (1, 0), vbbinarycompare)-1)
Do
Pdata. cfilename = string (260, 0)
Blnrc = internetfindnextfile (lnghinet, pdata)
If not blnrc then
Exit do
Else
Strtemp = left (pdata. cfilename, instr (1, pdata. cfilename, string (1, 0), vbbinarycompare)-1)
End if

Loop
Text1.text = strtemp
End if
End if
Internetclosehandle lnghinet

Internetclosehandle lnginetconn
Internetclosehandle lnginet


End sub

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.