Chilkat
HOME
Android™
ASP
Visual Basic
VB.NET
C#
iOS (IPhone)
Objective-C
C++
C
MFC
Delphi
FoxPro
Java
Perl
PHP Extension
PHP ActiveX
Python
PowerShell
Ruby
SQL Server
VBScript
Special Considerations w/ FTP Download Progress MonitoringDownload: Chilkat .NET Assemblies This example describes some special considerations with monitoring FTP file downloads. ' Our previous example (http://www.example-code.com/vbdotnet/ftp_upload_events.asp) discussed
' progress monitoring events in detail. The example will describe some special considerations
' when monitoring FTP downloads.
'
' In order to do percentage-done event callbacks, the FTP client needs to know
' how much data is expected to be downloaded. Most FTP servers will indicate the file
' size as part of the initial response to the RETR command sent by the FTP client.
' In those cases, Chilkat FTP2 monitors the download progress as expected and
' there is no problem.
'
' However, some FTP servers do not provide an indication of file size in response to
' RETR. In those cases it is necessary to get the file size from the remote directory
' listing, and then explicitly tell the FTP2 component the size of the download
' by setting the ProgressMonSize property.
'
' This example shows you how to do it:
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
Dim success As Boolean
success = ftpObj.UnlockComponent("Anything for 30-day trial")
If (Not success) Then
MessageBox.Show(ftpObj.LastErrorText)
Exit Sub
End If
' -----------------------------------------------
' IMPORTANT: Be sure to enable events on the object
' -----------------------------------------------
ftpObj.EnableEvents = True
' Set our heartbeat interval to .1 seconds:
ftpObj.HeartbeatMs = 100
' Set the FTP hostname, login, and password.
ftpObj.Hostname = "ftp.yourftpserver.com"
ftpObj.Username = "***"
ftpObj.Password = "***"
' Connect and login to the FTP server. Heartbeat events fire during the
' the connection establishment, authenticaion, and during all other communications
' with the FTP server.
success = ftpObj.Connect()
If (Not success) Then
MessageBox.Show(ftpObj.LastErrorText)
Exit Sub
End If
Dim localFilename As String
Dim remoteFilename As String
localFilename = "hamlet.xml"
remoteFilename = "hamlet.xml"
' Assume that this FTP server does NOT provide file size information
' during the download. We'll have to get it explicitly.
' We do so by callling GetSizeByName.
' Why are we setting the ListPattern? It's because the Chilkat FTP2
' component automatically retrieves directory information as needed
' and caches it in memory to minimize communications. By default, the
' ListPattern = "*". The GetSizeByName would have triggered a "LIST *"
' command to get a directory listing. If the directory contained a large
' number of files, the directory listing download could take a significant
' amount of time. To minimize the directory listing size, set the ListPattern
' to the exact filename so that it is the only file that matches.
' This directory listing size is minimal and we can get the size information
' from it.
' Remember: for most FTP servers, this is not necessary.
Dim fileSize As Integer
ftpObj.ListPattern = remoteFilename
fileSize = ftpObj.GetSizeByName(remoteFilename)
MessageBox.Show("File Size in Bytes = " & Convert.ToString(fileSize))
' Set the ProgMonSize to the size of the file being downloaded:
ftpObj.ProgressMonSize = fileSize
' Download a file.
success = ftpObj.GetFile(localFilename, remoteFilename)
If (Not success) Then
MessageBox.Show(ftpObj.LastErrorText)
Exit Sub
End If
' Disconnect
ftpObj.Disconnect()
' We're finished!
MessageBox.Show("FTP Download Finished!")
End Sub
|
© 2000-2012 Chilkat Software, Inc. All Rights Reserved.