Results 1 to 40 of 46

Thread: To all who's doing FTP: Here's an FTP class to simplify things drastically

Threaded View

  1. #1

    Thread Starter
    Fanatic Member jian2587's Avatar
    Join Date
    Aug 2000
    Location
    I bet u need a fusion powered shuttle to reach my place...
    Posts
    963

    Thumbs up [new update] Here's an FTP class to simplify things drastically

    ok this is the reinvented version I promised long time ago.
    there's a vbs script file that shows how to use the class file.
    see, no events needed. well, you can use the events too, by using the
    wscript.connectobject method.


    VB Code:
    1. 'To use this class, declare it like this
    2. Dim WithEvents ftpConn As ftpConnection
    3.  
    4. 'Create a new instance of it
    5. Set ftpConn = New ftpConnection
    6.  
    7. 'To connect
    8. ftpConn.User = "Anonymous"
    9. ftpConn.Password = "Guest"
    10. ftpConn.Passive = True ' Use passive mode
    11. ftpConn.RemoteHost = "ftp.ftpsite.com"
    12. ftpConn.RemotePort = 21 'default is 21
    13. ftpConn.Connect

    LISTING FILES + INFO
    VB Code:
    1. 'lstFiles is a listbox
    2.     If ftpConn.CommandState = CMD_LOGGED_IN Then
    3.         'if you want just a list of names, use ftpConn.GetFileList()
    4.         If ftpConn.GetFileListInfo() = True Then
    5.             Do Until ftpConn.ListParser.IsParsed
    6.                 DoEvents
    7.             Loop
    8.             Dim i As Long
    9.             lstFiles.Clear
    10.             For i = 0 To ftpConn.ListParser.FileCount - 1
    11.                 lstFiles.AddItem ftpConn.ListParser.fileList(i, ftpConn.ListParser.ColumnCount - 1)
    12.             Next
    13.         Else
    14.             MsgBox "FAILED"
    15.         End If
    16.     Else
    17.         MsgBox "NOT LOGGED IN"
    18.     End If

    OK, so here's the thing. My ftp class no longer reads from/writes to files. You've to supply the data when it's needed (through the appropriate raised events).

    DOWNLOAD
    VB Code:
    1. 'You must declare a dataConnection object
    2. Dim dataConn As dataConnection
    3.  
    4. 'Now you need to allocate a data socket
    5. Set dataConn = ftpConn.NewDataConnection(ftpConn.IsPassive)
    6. dataConn.DataType = TYPE_BINARY ' works for all types of files
    7.  
    8. 'To download
    9. If dataConn.Initiate(True) Then 'true to asynchronously connect
    10.     dataConn.StartTransfer TRANSFER_DOWNLOAD, "download.txt"
    11. End If
    12.  
    13. 'Incoming data event
    14. Private Sub dataConn_IncomingData(bytes As Long)
    15.     Dim s As String
    16.     s = dataConn.GetData() 'so now s holds (part of) the data
    17. End Sub
    18.  
    19. 'If you're writing s to a file and don't know when to stop, use this event
    20. Private Sub dataConn_StateChanged()
    21.     If dataConn.DataState = DATA_DISCONNECTED Then
    22.         dataConn.Dispose
    23.         Set dataConn = Nothing
    24.         'And close your file here
    25.     End If
    26. End Sub

    UPLOAD
    VB Code:
    1. 'You must declare a dataConnection object
    2. Dim dataConn As dataConnection
    3.  
    4. 'Now you need to allocate a data socket
    5. Set dataConn = ftpConn.NewDataConnection(ftpConn.IsPassive)
    6. dataConn.DataType = TYPE_BINARY ' works for all types of files
    7.  
    8. 'To upload
    9. If dataConn.Initiate(True) Then 'true to asynchronously connect
    10.     dataConn.StartTransfer TRANSFER_UPLOAD, "download.txt"
    11. End If
    12.  
    13. 'Upload data in this event, it'll be called every time the previous data
    14. 'chunk is fully uploaded
    15. Private Sub dataConn_NeedData()
    16.     If dataConn.DataState = DATA_TRANSFERRING Then
    17.         dataConn.SendData "Hello, World!" ' replace this data with sth else
    18.     End If
    19. End Sub
    20.  
    21. 'When you're done uploading, you can close the connection here
    22. Private Sub dataConn_SendComplete()
    23.     'If all data is uploaded then
    24.         'dataConn.Disconnect 'disconnect if you're done
    25.     'End If
    26. End Sub

    CLEANING UP
    VB Code:
    1. 'so you want to quit, do this.
    2. ftpConn.Quit 'Kiss FTP Server Bye bye
    3. ftpConn.Dispose 'Disconnect the sockets, destroy used objects
    4. dataConn.Dispose
    5. Set dataConn = Nothing
    6. Set ftpConn = Nothing

    To do other stuffs, like renaming/deleting/creating file/directory, etc, check out the class file. If there's some methods not provided, you can always use the SendCommand to send your own commands. Consult a FTP document.

    The attached file has demo codes, so you can see how things work. And finally, please do credit me whenever you use part or all of my codes.

    Thank you.
    Attached Files Attached Files
    Last edited by jian2587; Jan 11th, 2008 at 05:37 PM. Reason: New update
    ASM,C,C++,BASIC,VB,JAVA,VBS,HTML,ASP,PHP,mySQL,VB.NET,MATLAB
    Programming is fun, but only if you're not on a tight deadline
    So I consider all those working engineers sad people

    VB FTP class
    3 page PHP crash course
    Crash Course on DX9 Managed with VB.NET covering basics till terrain creation

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width