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
FTP Upload Directory TreeDownload: Chilkat .NET Assemblies VB.NET sample code to upload an entire local filesystem directory tree to an FTP server. ' Upload an entire directory tree to an FTP server. ' ' The upload is abortable at any point via the AbortCheck event callback. ' ' Three other event callbacks are available: ' OnBeginUploadFile - called before each file is uploaded, allowing ' specific files to be skipped. ' OnEndUploadFile - called after each file is uploaded. ' OnVerifyUploadDir - called before each sub-directory is entered, ' allowing entire sub-directories to be skipped. ' Don't forget to use WithEvents! Dim WithEvents ftpObj As New Chilkat.Ftp2 Dim AbortButtonPressed As Boolean ' This is the OnAbortCheck event callback. It is called periodically according ' to the HeartbeatMs property setting. Private Sub ftpObj_OnAbortCheck(ByVal sender As Object, ByVal args As Chilkat.AbortCheckEventArgs) Handles ftpObj.OnAbortCheck ' Was the Abort button pressed? If (AbortButtonPressed) Then ' To abort the current FTP operation, set Abort = true args.Abort = True End If ' Update a progress bar so we can visually see the heartbeat: If (ProgressBar1.Value < 100) Then ProgressBar1.Value = ProgressBar1.Value + 10 Else ProgressBar1.Value = 0 End If ' Handle application events so our user interface remains responsive. Application.DoEvents() End Sub ' Our user-interface has an abort button which when pressed, ' sets the AbortCurrentFtpOp boolean to True. If an FTP operation ' is in progress, it will be aborted at the next heartbeat. Private Sub AbortButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AbortButton.Click AbortButtonPressed = True End Sub ' Called before each file is uploaded Private Sub ftpObj_OnBeginUploadFile(ByVal sender As Object, ByVal args As Chilkat.FtpTreeEventArgs) Handles ftpObj.OnBeginUploadFile ' This example will skip all filenames ending in ".obj" If (args.Path.EndsWith(".obj")) Then args.Skip = True End If End Sub ' Called after each file is uploaded. Private Sub ftpObj_OnEndUploadFile(ByVal sender As Object, ByVal args As Chilkat.FtpTreeEventArgs) Handles ftpObj.OnEndUploadFile ' Add the filepath and size of file to a listbox. ListBox1.Items.Add(args.Path & ": " & Convert.ToString(args.NumBytes)) ListBox1.Refresh() End Sub ' Called before each local sub-directory is entered. ' This callback may be called twice for a single directory for technical reasons. Private Sub ftpObj_OnVerifyUploadDir(ByVal sender As Object, ByVal args As Chilkat.FtpTreeEventArgs) Handles ftpObj.OnVerifyUploadDir ListBox1.Items.Add("DIR: " + args.Path) ListBox1.Refresh() ' Skip all directories having "temp" in the path If (args.Path.Contains("/temp/")) Then args.Skip = True End If End Sub ' Code to upload directory tree to FTP server: Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.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 ' Navigate to the root of the remote directory tree ' where the local directory tree will be re-created. success = ftpObj.ChangeRemoteDir("a") If (Not success) Then success = ftpObj.CreateRemoteDir("a") If (Not success) Then MessageBox.Show(ftpObj.LastErrorText) Exit Sub Else success = ftpObj.ChangeRemoteDir("a") If (Not success) Then MessageBox.Show(ftpObj.LastErrorText) Exit Sub End If End If End If ' Upload the entire local directory tree to the current remote directory. success = ftpObj.PutTree("c:/temp/a") If (Not success) Then MessageBox.Show(ftpObj.LastErrorText) Exit Sub End If ' Disconnect ftpObj.Disconnect() ' We're finished! MessageBox.Show("FTP Upload Tree Finished!") End Sub |
© 2000-2012 Chilkat Software, Inc. All Rights Reserved.