Posted on : 13-01-2011 | By : ocx | In : Develop
Please note: this how-to article refer to software developers and software authors.
This demonstrate will show you how to add FTP functionality to your program using the FTP Client ActiveX.
Requirements:
- FTP Client ActiveX
- VB6 / VB.NET 2008 or 2010
This sample using VB.NET. The VB6 sample existed under the installation program of the component.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 | Private Sub chkAnonym_CheckStateChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles chkAnonym.CheckStateChanged If chkAnonym.CheckState Then txtUserName.Text = "anonymous" txtPassword.PasswordChar = CChar("") txtPassword.Text = "guest@unknow.com" Else txtUserName.Text = "" txtPassword.PasswordChar = CChar("*") txtPassword.Text = "" End If End Sub Private Sub cmdViewFile_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdViewFile.Click 'Download the file then open the file. End Sub Private Sub frmMain_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load 'Init the control: 'Must be call before using any 'functionality of the control. 'sKey = Your registration key. 'On trial mode use the "Trial Mode." cFtp1.Init ("Trial Mode.") End Sub Private Sub frmMain_Closed(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Closed 'Release the control 'from the memory: cFtp1.DeInit() End Sub Private Sub cmdConnect_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdConnect.Click 'Connect to the FTP server On Error GoTo ErrConnect 'Clear the 'ftp://' string from the address (if any) If LCase(VB.Left(txtServer.Text, Len("ftp://"))) = "ftp://" Then txtServer.Text = Mid(txtServer.Text, Len("ftp://") + 1) End If 'Clear the 'http://' string from the address (if any) If LCase(VB.Left(txtServer.Text, Len("http://"))) = "http://" Then txtServer.Text = Mid(txtServer.Text, Len("http://") + 1) End If 'Set the FTP server properties: cFtp1.FtpRemotePort = Val(txtRemotePort.Text) cFtp1.FtpServer = txtServer.Text cFtp1.FtpUserName = txtUserName.Text cFtp1.FtpPassword = txtPassword.Text cFtp1.FtpPassiveMode = CBool(chkPassive.CheckState) cFtp1.Timeout = Val(txtTimeOut.Text) 'Connect to the server: cFtp1.Connect() 'Get the main path cFtp1.PathDir ("/") Exit Sub 'On Error ErrConnect: MsgBox (Err.Description) 'Or cFtp1.GetFtpErrorDescription(Err.Number) End Sub Private Sub cmdDeleteDir_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdDeleteDir.Click 'Delete a directory from the FTP server Dim strDirName As String strDirName = cmbRemote.Text & "/" & Trim(VB6.GetItemString(lstRemote, lstRemote.SelectedIndex)) 'Check if this is a directory If cFtp1.RemoveDirectory(strDirName) = True Then 'Success, reload the directory ReloadDir() Else 'An error: display a message MsgBox("Can't remove directory from the FTP server." & vbCrLf & vbCrLf & "FTP server: " & cFtp1.GetLastServerResponse, MsgBoxStyle.Information, "Can't remove directory from the FTP server.") End If End Sub Private Sub cmdDeleteFile_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdDeleteFile.Click 'Delete a file from the FTP server Dim strFileName As String strFileName = cmbRemote.Text & "/" & Trim(VB6.GetItemString(lstRemote, lstRemote.SelectedIndex)) 'Check if the item is a file If cFtp1.DeleteFile(strFileName) = True Then 'Success, Reload the directory ReloadDir() Else 'An error: display a message MsgBox("Can't delete file." & vbCrLf & vbCrLf & "FTP server: " & cFtp1.GetLastServerResponse, MsgBoxStyle.Information, "Can't delete file from the FTP server") End If End Sub Private Sub cmdDisconnect_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdDisconnect.Click 'Disconnect the FTP server cFtp1.Disconnect() End Sub Private Sub cmdNewDir_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdNewDir.Click 'Create a new directory in the FTP server under 'the the current directory Dim strDirName As String strDirName = InputBox("Please enter a directory name.", "Create new directory") If Len(strDirName) > 0 Then If cFtp1.CreateDirectory(strDirName) = True Then 'Success, Reload the directory ReloadDir() Else 'An error: display a message MsgBox("Can't create new directory on the FTP server." & vbCrLf & vbCrLf & "FTP server: " & cFtp1.GetLastServerResponse, MsgBoxStyle.Information, "Can't create directory") End If End If End Sub Private Sub cmdRefresh_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdRefresh.Click 'Display the files ReloadDir() End Sub Private Sub cmdRename_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdRename.Click 'Rename file/directory Dim strName As String Dim strNewName As String Dim intAnswer As Short On Error GoTo ErrCmdRename 'Get the name to rename strName = VB6.GetItemString(lstRemote, lstRemote.SelectedIndex) 'Get the new name strNewName = InputBox("Please enter new name for " & strName, "Rename") If Len(strNewName) > 0 Then 'Ask the user intAnswer = MsgBox("Rename " & strName & " to " & strNewName & "?", MsgBoxStyle.YesNo + MsgBoxStyle.Question, "Rename File") If intAnswer = MsgBoxResult.Yes Then 'Rename If cFtp1.Rename(strName, strNewName) = True Then ReloadDir() Else 'An error apears MsgBox("Can't rename the file." & vbCrLf & vbCrLf & "Server response: " & cFtp1.GetLastServerResponse, , "Can't rename file") End If End If End If Exit Sub ErrCmdRename: If Err.Number = 91 Then MsgBox("Please select a file to rename.", MsgBoxStyle.Information, "Rename File") Else MsgBox("Error:" & vbCrLf & "#" & Err.Number & ": " & Err.Description, MsgBoxStyle.Information, "Rename File") End If End Sub Private Sub cmdUploadFile_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdUploadFile.Click 'Upload a file to the FTP server Dim sFile As String Dim strFileName As String Dim iTransMode As FTP_CLIENT_ACTIVEX.FtpTransferMode Dim lStartPoint As Integer Dim lFileSize As Integer With CommonDialog1 'Select the file to upload .DialogTitle = "Select file to upload" .CancelError = True .Filter = "All Files (*.*)|*.*" 'Open the dialog .ShowOpen() If Err.Number = 0 Then If Len(.FileName) = 0 Then Exit Sub End If 'Get the file to upload sFile = Mid(.FileName, InStrRev(.FileName, "\") + 1) 'File type upload mode If Option1(0).Checked = True Then iTransMode = FTP_CLIENT_ACTIVEX.FtpTransferMode.FTP_ASCII_MODE Else iTransMode = FTP_CLIENT_ACTIVEX.FtpTransferMode.FTP_IMAGE_MODE End If 'Upload the file If cFtp1.UploadFile(.FileName, sFile, iTransMode, lStartPoint) = True Then ReloadDir() Else 'Error MsgBox("Can't upload file." & vbCrLf & vbCrLf & "Server response: " & cFtp1.GetLastServerResponse, , "Can't upload file") End If 'The operation has done. MsgBox ("DONE.") End If End With End Sub Public Function ReloadDir() As Object 'Reload directory: lstRemote.Items.Clear() cFtp1.PathDir (cmbRemote.Text) End Function Private Sub cmdDownload_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdDownload.Click 'Download a file from the FTP server: On Error Resume Next Dim sFileSize As String Dim lStartPoint As Integer Dim bForceDownload As Boolean Dim vTransferMode As FTP_CLIENT_ACTIVEX.FtpTransferMode Dim sFile As String Dim lFileSize As Integer Dim intRetVal As Short 'Display a Save As dialog CommonDialog1.DialogTitle = "Save as..." CommonDialog1.Filter = "All Files (*.*)|*.*" CommonDialog1.CancelError = True CommonDialog1.FileName = VB6.GetItemString(lstRemote, lstRemote.SelectedIndex) CommonDialog1.ShowSave() If Err.Number = 0 Then If Len(CommonDialog1.FileName) = 0 Then Exit Function End If 'Get the info sFile = VB6.GetItemString(lstRemote, lstRemote.SelectedIndex) lFileSize = CInt(VB6.GetItemString(lstRemote, lstRemote.SelectedIndex)) 'File type mode If CBool(Option1(0).Checked) Then vTransferMode = FTP_CLIENT_ACTIVEX.FtpTransferMode.FTP_ASCII_MODE Else vTransferMode = FTP_CLIENT_ACTIVEX.FtpTransferMode.FTP_IMAGE_MODE End If TryAgain: 'Download the file If Not cFtp1.DownloadFile(sFile, CommonDialog1.FileName, vTransferMode, lStartPoint) Then 'Check if there is an error: BadState If cFtp1.FtpGetLastError = FTP_CLIENT_ACTIVEX.FtpError.ERROR_FTP_WINSOCK_BadState Then 'The connection is broken intRetVal = MsgBox("The connection to the FTP server is broken. Establish the connect again?", MsgBoxStyle.Question + MsgBoxStyle.YesNo) If intRetVal = MsgBoxResult.Yes Then 'So try again If cFtp1.Connect Then If cFtp1.SetCurrentDirectory(cmbRemote.Text & "/" & Trim(VB6.GetItemString(lstRemote, lstRemote.SelectedIndex))) Then GoTo TryAgain End If Else 'Error: The connection cannot be established. MsgBox("The connection to the FTP server cannot established.", MsgBoxStyle.Exclamation) End If Else 'Call ResetProgress End If 'Check if there is an error: Timeout over ElseIf cFtp1.FtpGetLastError = FTP_CLIENT_ACTIVEX.FtpError.ERROR_FTP_USER_TIMEOUT Then 'The server is not responding intRetVal = MsgBox("The FTP server is not responding. Try again?", MsgBoxStyle.YesNo + MsgBoxStyle.Question) 'Try again: If intRetVal = MsgBoxResult.Yes Then GoTo TryAgain Else 'Call ResetProgress End If Else 'Error MsgBox("Error #" & cFtp1.FtpGetLastError & vbCrLf & vbCrLf & cFtp1.GetFtpErrorDescription(cFtp1.FtpGetLastError), MsgBoxStyle.Exclamation) End If End If MsgBox ("DONE.") End If End Function Private Sub lstRemote_DoubleClick(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles lstRemote.DoubleClick 'Redirection: Dim sDir As String 'Get the dir sDir = Trim(VB6.GetItemString(lstRemote, lstRemote.SelectedIndex)) 'Clear the list lstRemote.Items.Clear() 'Set the remote address cmbRemote.Text = cmbRemote.Text & "/" & sDir 'Open the FTP server directory cFtp1.PathDir (cmbRemote.Text) End Function '************************************************************ 'Events: '************************************************************ Private Sub cFtp1_DownloadProgress(ByVal eventSender As System.Object, ByVal eventArgs As AxFTP_CLIENT_ACTIVEX.__cFtp_DownloadProgressEvent) Handles cFtp1.DownloadProgress 'The download progress lblDownloadProgress.Text = CStr(eventArgs.lBytes) End Function Private Sub cFtp1_UploadProgress(ByVal eventSender As System.Object, ByVal eventArgs As AxFTP_CLIENT_ACTIVEX.__cFtp_UploadProgressEvent) Handles cFtp1.UploadProgress 'The upload progress lblUploadProgress.Text = CStr(eventArgs.lBytes) End Function Private Sub cFtp1_PathEvent(ByVal eventSender As System.Object, ByVal eventArgs As AxFTP_CLIENT_ACTIVEX.__cFtp_PathEvent) Handles cFtp1.PathEvent 'Display current FTP server path lstRemote.Items.Add (eventArgs.sName) End Function Private Sub cFtp1_Stat(ByVal eventSender As System.Object, ByVal eventArgs As AxFTP_CLIENT_ACTIVEX.__cFtp_StatEvent) Handles cFtp1.Stat 'The stat of the FTP server connection txtStatus.Text = txtStatus.Text & eventArgs.sMessage & vbCrLf End Function Private Sub cFtp1_Error(ByVal eventSender As System.Object, ByVal eventArgs As AxFTP_CLIENT_ACTIVEX.__cFtp_ErrorEvent) Handles cFtp1.Error 'Error txtStatus.Text = eventArgs.sErrMessage & "(" & eventArgs.lErrNumber & ")" & vbCrLf End Function Private Sub lstRemote_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstRemote.SelectedIndexChanged End Function |


0