Page 1 of 1

FTP download with progress?

Posted: Sun Jun 05, 2011 1:28 pm
by Metzger
Hey guys, I am wondering if anyone can give me an example on how to download file from FTP with full progress status.
Tryed it with FTPProgress() function but it seems to be not that what I want. Any idea or any tip how to manage it?

PS. Also tryed FTP-Userlib but it make me probs. under x64 bit version of Win7 and I dont really have an idea how to compile it with for x64.

Any help is welcome. Thanks in advance!

Re: FTP download with progress?

Posted: Mon Jun 06, 2011 8:58 am
by Tranquil
Maybe its better to code your own FTP commands. (This is what I do when it comes to networking all the time)
You can find some source codes in this forum or take a look on www.purearea.net

Sorry, can not supply a source yet as I'm in office right now.

Re: FTP download with progress?

Posted: Mon Jun 06, 2011 12:48 pm
by IdeasVacuum
Something like this?

Code: Select all

InitNetwork()

#ID_WINDOW  = 0
#Progress   = 1
#Ftp        = 2
#BtnExit    = 3
#BtnAbort   = 4
#BtnGo      = 5
#Alert      = 6
#BtnGetFile = 7
#File       = 8

iUploading = 0
iExit = 0

 If OpenWindow(#ID_WINDOW, 0, 0, 240, 240, "FTP", #PB_Window_SystemMenu| #PB_Window_SizeGadget| #PB_Window_MinimizeGadget| #PB_Window_TitleBar| #PB_Window_MaximizeGadget| #PB_Window_ScreenCentered)

      ProgressBarGadget(#Progress,    20,  10, 200, 18, 0, 100)
             TextGadget(#Alert,       20,  50, 200, 25, "FTP Failed/Aborted",#PB_Text_Center)
           StringGadget(#File,        20,  80, 200, 25, "")
           ButtonGadget(#BtnGetFile,  20, 105, 200, 25, "File")
           ButtonGadget(#BtnGo,       20, 140, 200, 25, "Upload")
           ButtonGadget(#BtnAbort,    20, 180, 200, 25, "Abort")
           ButtonGadget(#BtnExit,     20, 205, 200, 25, "Exit")
             HideGadget(#Alert,1)
         SetGadgetState(#Progress,0)

EndIf

iInitialise = OpenFTP(#Ftp,"Your Host", "Your User", "Your Password", 21)

If iInitialise = 0

        MessageRequester("Error", "Cannot connect to the FTP server")
EndIf

  Repeat

                       iEvent = WaitWindowEvent(1)
                    If iEvent = #PB_Event_Gadget

                                    iGdgID = EventGadget()
                             Select iGdgID

                                     Case #BtnAbort
                                                  If iInitialise > 0 : AbortFTPFile(#Ftp) : EndIf
                                                  HideGadget(#Alert,0)

                                     Case #BtnGetFile
                                                  If iInitialise > 0
                                                               sFile.s = OpenFileRequester("Choose a file to send", "", "*.*", 0)
                                                           sFileName.s = GetFilePart(sFile)
                                                          qMaxProgress = FileSize(sFile)
                                                      SetGadgetAttribute(#Progress, #PB_ProgressBar_Maximum, qMaxProgress)
                                                           SetGadgetText(#File,sFileName)
                                                           
                                                  EndIf

                                     Case #BtnGo
                                                  iSendResult.i = SendFTPFile(#Ftp,sFile,sFileName,#True)
                                                     iUploading = 1

                                     Case #BtnExit
                                                  iExit = 1
                                                  If iInitialise > 0 : AbortFTPFile(#Ftp) : EndIf
                             EndSelect
                    EndIf

                    If iUploading = 1
                                      SetGadgetState(#Progress,FTPProgress(#Ftp))
                    EndIf
  Until iExit = 1

  If iUploading = 1
                     If FTPProgress(#Ftp) = #PB_FTP_Finished

                                 SetGadgetState(#Progress,100)
                                  SetGadgetText(#Alert,"File Uploaded")
                                     HideGadget(#Alert,0)
                     EndIf

                     If FTPProgress(#Ftp) = #PB_FTP_Error : HideGadget(#Alert,0) : EndIf
                     If iSendResult = 0 : HideGadget(#Alert,0) : EndIf
  EndIf

  If iExit = 1
                End
  EndIf
Edit: Set Progress Bar Max to length of file to be ftp'd
Ah, this example is of course upload rather than download - you will want to connect to the site, then Examine Directory if your app is intended to be flexible, otherwise just hard-code the file name.

Re: FTP download with progress?

Posted: Mon Jun 06, 2011 4:35 pm
by TerryHough
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