Progress Bar on Ftp Upload

freuzo

Member
Local time
Today, 17:01
Joined
Apr 14, 2020
Messages
98
Hi,

I have this code to upload files to a server.

Code:
Option Compare Database
Option Explicit
Option Private Module

'-------------------
'API Declaration
'-------------------
Private Declare Function InternetCloseHandle Lib "wininet.dll" _
  (ByVal hInet As Long) As Integer
 
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
 
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
 
Private Declare Function FtpSetCurrentDirectory Lib "wininet.dll" Alias _
  "FtpSetCurrentDirectoryA" (ByVal hFtpSession As Long, _
  ByVal lpszDirectory As String) As Boolean
 
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
 
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
 
 
'To send a file
Public Function FtpUpload(SourceFile As String, DestFile As String)
    Dim HwndConnect As Long
    Dim HwndOpen As Long

    'Open internet
    HwndOpen = InternetOpen("SiteWeb", 0, vbNullString, vbNullString, 0)

    'Connection to ftp
    HwndConnect = InternetConnect(HwndOpen, "111.111.44.55", 21, "ubuntu", "Password", 1, 0, 0)

    FtpSetCurrentDirectory HwndConnect, "/home/ubuntu/ftp/files"

    'Send file "SourceFile" and rename it "DestiFile" on the server
    FtpPutFile HwndConnect, SourceFile, DestFile, &H0, 0

    InternetCloseHandle HwndConnect 'close connection
    InternetCloseHandle HwndOpen 'close internet
End Function

It works fine.
Now I need to add a progress bar to let the users know that the app is still working and isn't just dead. Something that will start at the beginning of this procedure and end at his end.

Thanks in advance.
 
Try searching this site, several version around, I seem to recall.
Not sure how you intend to increment it though? :unsure:
 
Because FTP is being treated "monolithically" you have no handle on progress. You cannot see how much of the file remains to be sent. That "FtpPutFile" is a whole-file transfer operation, not a "part of a file" operation.

I might put a timer on the form such that every second or so you flip colors or do something visually IN A TIMER ROUTINE to show that you are still there.
 
Try searching this site, several version around, I seem to recall.
Not sure how you intend to increment it though? :unsure:
I found some samples here without knowing how to implement any of them.

Because FTP is being treated "monolithically" you have no handle on progress. You cannot see how much of the file remains to be sent. That "FtpPutFile" is a whole-file transfer operation, not a "part of a file" operation.

I might put a timer on the form such that every second or so you flip colors or do something visually IN A TIMER ROUTINE to show that you are still there.
That's an interesting idea.
 
I found some samples here without knowing how to implement any of them.


That's an interesting idea.
Perhaps just have "Uploading " and then add a . after every timer interval ?
 
Yes. to download the file "test.txt" to "c\temp" folder you change that line
FtpPutFile HwndConnect, SourceFile, DestFile, &H0, 0
to
Code:
    FtpGetFile HwndConnect, "test.txt", "C:\temp\test.txt", False, 0, &H0, 0
 
Thanks freuzo/gasman

Got it
Private Const FILE_ATTRIBUTE_NORMAL As Long = &H80
Private Const FTP_TRANSFER_TYPE_BINARY As Long = &H2


'Download File
Flags = &H0
FtpGetFile HwndConnect, SourceFile, DestFile, Flags, FILE_ATTRIBUTE_NORMAL, FTP_TRANSFER_TYPE_BINARY, 0
 
Last edited:
It is for vb.net, so not sure how compatible it would be
Its not for vb.net (language)!

That site is called VBnet.org (or was before it was re-homed in mvps.org) and is for VB6, but was created and named before vb.net (language) was even born!
 
Last edited:
Its not for vb.net (language)!

That site is called VBnet.org (or was before it was re-homed in mvps.org) and is for VB6, but was created and named before vb.net (language) was even born!
Ah OK, my apologies. :(
 

Users who are viewing this thread

Back
Top Bottom