Here is an example of a FTP download with progress for you.
Code: Select all
; FTP_Receive_A_File - terryhough
;Host parameters fictional for exercise - substitute your own legitimate parameters
IPAddress.s = "ftp.server.com"
Username.s = "USERNAME"
Password.s = "PASSWORD"
Procedure FindFileSize(FTPC.l,FileNameToFind.s) ; Find the file's details at the FTP server.
If CheckFTPConnection(FTPC) ; Check to see if connection is still open
CurDir$ = GetFTPDirectory(FTPC)
If ExamineFTPDirectory(FTPC)
While NextFTPDirectoryEntry(FTPC)
If FTPDirectoryEntryName(FTPC) = FileNameToFind
SearchFileSize.l = FTPDirectoryEntrySize(FTPC)
; Use to display complete file info from FTP server
AddGadgetItem(1,-1, "")
AddGadgetItem(1,-1, "Check the server file details.")
Hdr$ = LSet("Filename",15) + " "
Hdr$ + LSet("Date", 19) + " "
Hdr$ + LSet("Bytes",9) + " "
Hdr$ + "Type"
AddGadgetItem(1,-1, Hdr$)
AddGadgetItem(1,-1, "-----------------------------------------------------")
Msg$ = LSet(FTPDirectoryEntryName(FTPC),15) + " "
Msg$ + FormatDate("%mm/%dd/%yyyy %hh:%ii:%ss", FTPDirectoryEntryDate(FTPC)) + " "
Msg$ + RSet(Str(SearchFileSize),9) + " "
Msg$ + Str(FTPDirectoryEntryType(FTPC))
AddGadgetItem(1,-1, Msg$)
AddGadgetItem(1,-1, "-----------------------------------------------------")
AddGadgetItem(1,-1, "")
SetGadgetState(1,CountGadgetItems(1)-1)
While WindowEvent() : Wend
; --- End display of file's info from FTP server ---
FinishFTPDirectory(FTPC)
ProcedureReturn SearchFileSize
EndIf
Wend
FinishFTPDirectory(FTPC)
ProcedureReturn -1 ; Target file not found
Else
ProcedureReturn -2 ; Can't examine the directory
EndIf
Else
ProcedureReturn -3 ; Connection is not open
EndIf
EndProcedure
;First initialize the network
hwnd=OpenWindow(0, 0, 0, 630, 450, "FTP Library Test", #PB_Window_SystemMenu)
;CreateGadgetList(WindowID(0))
ListViewGadget(1, 4, 10, 622, 400)
While WindowEvent() : Wend
While WindowEvent() : Wend
If LoadFont(1, "COURIER", 12)
SetGadgetFont(1, FontID(1))
EndIf
TextGadget(2, 4, 420, 622, 15,"")
ProgressBarGadget(41, 4, 435, 622, 10, 0, 100,#PB_ProgressBar_Smooth)
AddGadgetItem(1,-1,"Starting actvity")
While WindowEvent() : Wend
InitNetwork()
AddGadgetItem(1,-1,"Initialized the network")
While WindowEvent() : Wend
Connect.l = OpenFTP(#PB_Any, IPAddress, Username, Password)
If Connect
AddGadgetItem(1,-1,"Opened FTP Session " + Str(Connect))
SetFTPDirectory(Connect,"..")
Directoryname$ = GetFTPDirectory(Connect)
AddGadgetItem(1,-1,"Current directory is: " + Directoryname$)
While WindowEvent() : Wend
; SetFTPDirectory() - only an immediate directory change is allowed.
; To change to several levels of directory, this command will have to be called several time.
; Eg. SetFtpDirectory(Connect, "pub/support") is invalid on most servers.
ServerDirectory$ = "/receive/recd" ; replace with your information <<<<<<<------
For Ctr = 2 To CountString(ServerDirectory$, "/")+1
SetFTPDirectory(Connect, StringField(ServerDirectory$,Ctr,"/"))
Next
Directoryname$ = GetFTPDirectory(Connect)
AddGadgetItem(1,-1,"Changed the directory to " + Directoryname$)
While WindowEvent() : Wend
Filename.s = "C:\brochure.pdf" ; <---- put your filename to be downloaded here
Filelen.l = FindFileSize(Connect,GetFilePart(Filename))
AddGadgetItem(1,-1, "")
AddGadgetItem(1,-1,"Get file from server: " + GetFilePart(Filename) + " - " + Str(Filelen) + " bytes")
While WindowEvent() : Wend
ReceiveFTPFile(Connect, GetFilePart(Filename), Filename, 1)
Repeat
Delay(10)
Until FTPProgress(Connect) = #PB_FTP_Started
Msg$ = "The file transfer has been initialized."
AddGadgetItem(1,-1, Msg$)
SetGadgetState(1,CountGadgetItems(1)-1)
While WindowEvent() : Wend
Repeat
Result = FTPProgress(Connect)
Select Result
Case #PB_FTP_Error
Msg$ = "Send/Recv error occurred."
AddGadgetItem(1,-1, Msg$)
Break
Case #PB_FTP_Finished
Msg$ = "Finished flag received."
SetGadgetText(2,"Bytes received: " + Str(FileLen) + " (100%)")
SetGadgetState(41, 100)
Break
Default
Progress.f = Round((Result / Filelen)*100, #True)
SetGadgetText(2,"Bytes received: " + Str(Result) + " of " + Str(FileLen) + " (" + StrF(Progress,1)+"%)")
SetGadgetState(41, Progress)
While WindowEvent() : Wend
EndSelect
Delay(10)
ForEver
AddGadgetItem(1,-1, Msg$)
SetGadgetState(1,CountGadgetItems(1)-1)
While WindowEvent() : Wend
Else
MessageRequester("FTP Library Test", "Failed to Connect", #MB_ICONERROR)
End
EndIf
While WindowEvent() : Wend
Repeat : Delay(5) : Until WaitWindowEvent() = #PB_Event_CloseWindow
CloseFTP(Connect)
End