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:
'To use this class, declare it like this Dim WithEvents ftpConn As ftpConnection 'Create a new instance of it Set ftpConn = New ftpConnection 'To connect ftpConn.User = "Anonymous" ftpConn.Password = "Guest" ftpConn.Passive = True ' Use passive mode ftpConn.RemoteHost = "ftp.ftpsite.com" ftpConn.RemotePort = 21 'default is 21 ftpConn.Connect
LISTING FILES + INFO
VB Code:
'lstFiles is a listbox If ftpConn.CommandState = CMD_LOGGED_IN Then 'if you want just a list of names, use ftpConn.GetFileList() If ftpConn.GetFileListInfo() = True Then Do Until ftpConn.ListParser.IsParsed DoEvents Loop Dim i As Long lstFiles.Clear For i = 0 To ftpConn.ListParser.FileCount - 1 lstFiles.AddItem ftpConn.ListParser.fileList(i, ftpConn.ListParser.ColumnCount - 1) Next Else MsgBox "FAILED" End If Else MsgBox "NOT LOGGED IN" 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:
'You must declare a dataConnection object Dim dataConn As dataConnection 'Now you need to allocate a data socket Set dataConn = ftpConn.NewDataConnection(ftpConn.IsPassive) dataConn.DataType = TYPE_BINARY ' works for all types of files 'To download If dataConn.Initiate(True) Then 'true to asynchronously connect dataConn.StartTransfer TRANSFER_DOWNLOAD, "download.txt" End If 'Incoming data event Private Sub dataConn_IncomingData(bytes As Long) Dim s As String s = dataConn.GetData() 'so now s holds (part of) the data End Sub 'If you're writing s to a file and don't know when to stop, use this event Private Sub dataConn_StateChanged() If dataConn.DataState = DATA_DISCONNECTED Then dataConn.Dispose Set dataConn = Nothing 'And close your file here End If End Sub
UPLOAD
VB Code:
'You must declare a dataConnection object Dim dataConn As dataConnection 'Now you need to allocate a data socket Set dataConn = ftpConn.NewDataConnection(ftpConn.IsPassive) dataConn.DataType = TYPE_BINARY ' works for all types of files 'To upload If dataConn.Initiate(True) Then 'true to asynchronously connect dataConn.StartTransfer TRANSFER_UPLOAD, "download.txt" End If 'Upload data in this event, it'll be called every time the previous data 'chunk is fully uploaded Private Sub dataConn_NeedData() If dataConn.DataState = DATA_TRANSFERRING Then dataConn.SendData "Hello, World!" ' replace this data with sth else End If End Sub 'When you're done uploading, you can close the connection here Private Sub dataConn_SendComplete() 'If all data is uploaded then 'dataConn.Disconnect 'disconnect if you're done 'End If End Sub
CLEANING UP
VB Code:
'so you want to quit, do this. ftpConn.Quit 'Kiss FTP Server Bye bye ftpConn.Dispose 'Disconnect the sockets, destroy used objects dataConn.Dispose Set dataConn = Nothing 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.






Reply With Quote