-
[RESOLVED] Possible to download/upload files into FTP using WinSock 2.0 or CSocket ?
As the topic says..
Is there any way to use one of those 2 controlers to upload files to ftp?
Also would be great if there exists some module/class for MsInet
I didn't wanted to use ocx files in this ftp client.
-
1 Attachment(s)
Re: Possible to download/upload files into FTP using WinSock 2.0 or CSocket ?
The attached ZIP file contains a bunch of classes I wrote a few years back that you can use for FTP. Even though it contains 5 classes and one module and you'll have to add them all to your project, you only create one object (an instance of CFtp). Here's how to use it (please read the comments below):
VB Code:
Private [b]WithEvents [/b]oFTP As CFtp
Private Sub Form_Load()
Set oFTP = New CFtp
'All arguments to the Connect method is optional
'you can set them via properties instead
oFTP.Connect "ftp.mysite.com", "username", "password"
' Set the TransferType property to determent how a file
' is transfered as ASCII (converts newlines) or as Binary
oFTP.TransferType = FTP_TRANSFER_TYPE_ASCII 'or FTP_TRANSFER_TYPE_BINARY
' The UploadFile raises events while uploading
' but the call will still not return until its done.
' You can however cancel the transfer in the TransferProgress
' event if you like by setting the CancelProperty to True
oFTP.UploadFile "c:\myfile.txt", "newfile.txt"
' You can also use the PutFile method to upload a file,
' however this method does not raise any events and does not
' give you the option to cancel the transfer:
'oFTP.PutFile "c:\LocalFile.txt", "RemoteName.txt"
' To download a file simply use the DownloadFile method
' if you want the events to fire or the GetFile method if
' you are not interested in the progress.
'
' The following shows how you can list the files in the current
' remote directory into a listbox.
Dim oFile As CFtpFile
' First argument: File pattern (use * instead of *.* on Unix/Linux)
' Second argument: (Optional) NoCache
oFTP.ListDir "*", True '<- Second argument (optional): NoCache
For Each oFile In oFTP.Files
If oFile.IsDirectory Then
List1.AddItem "[" & oFile.FileName & "]"
Else
List1.AddItem oFile.FileName
End If
Next
' The CFtp class has a bunch of other method and properties such as
' MakeDir, ChangeDir, DeleteDir, DeleteFile, FileExist, GetCurrDir
' .. and so on
End Sub
Private Sub oFTP_ErrorMsg(ByVal sErr As String)
'Error messages
Debug.Print "Error: " & sErr
End Sub
Private Sub oFTP_StatusMsg(ByVal Msg As String)
'different status messages
Debug.Print Msg
End Sub
Private Sub oFTP_TransferCompleted()
'a download/upload is completed
End Sub
Private Sub oFTP_TransferProgress(ByVal nPercent As Long, _
ByVal fBytesReceived As Double, ByVal fFileSize As Double)
'You can use this event to update a progress bar or
'to give some other feedback to the user about the
'download/upload progress.
End Sub
-
Re: Possible to download/upload files into FTP using WinSock 2.0 or CSocket ?
KO!!
Awsome work you have donne!
The only doubt i have is how do you close the connection. Or will it timeout?
-
Re: Possible to download/upload files into FTP using WinSock 2.0 or CSocket ?
No, it will not timeout by itself, however the server might close an inactive connection after X number of minutes. However you should do it by calling the CloseConnection method.
-
Re: Possible to download/upload files into FTP using WinSock 2.0 or CSocket ?
I wonder how didn't see that :\
My bad, i should have more attention when reading
-
Re: [RESOLVED] Possible to download/upload files into FTP using WinSock 2.0 or CSocket ?
Is there any way to make the the oftp wait till the upload is donne?
like with inet.stillexecuting
-
Re: [RESOLVED] Possible to download/upload files into FTP using WinSock 2.0 or CSocket ?
Create your own boolean variable, for example blnFTPExecuting, and set it to True when you start an upload or download. Then reset it to False in the TransferCompleted event.... Or, if you're not interested in the events use the PutFile function instead, that will not return until the file is uploaded, but you can of course in that case not show any progress either.
-
Re: [RESOLVED] Possible to download/upload files into FTP using WinSock 2.0 or CSocket ?
thanks for the tip ;)
working and runing now as it should
-
Re: [RESOLVED] Possible to download/upload files into FTP using WinSock 2.0 or CSocket ?
Is there a way to do this on a different port? Would I just do it like this?
oFTP.Connect "ftp://mrtikki2.no-ip.org:2121", "Username", "Password"
-
Re: [RESOLVED] Possible to download/upload files into FTP using WinSock 2.0 or CSocket ?
You'll need to make a slight change to the Connect method to allow that.
-
Re: [RESOLVED] Possible to download/upload files into FTP using WinSock 2.0 or CSocket ?
Public Function Connect(Optional RemoteHost As String, Optional UserName As String, Optional Password As String, Optional nPort As String, Optional ProxyName As String) As Boolean
I got it.. thanks for the advice!
-
Re: [RESOLVED] Possible to download/upload files into FTP using WinSock 2.0 or CSocket ?
Alright, now I'm having a little bit of trouble with the send file.. I'm running a personal ftp client and I'm allowing people to send files to me. I see myself connect, but it disconnects very fast and doesn't send the file. Here's my code. Thanks for the help.
File = Format(Date, "mm.dd.yy") & " ~ " & Format(Time, "hh.mm.ss")
Set oFTP = New CFtp
oFTP.Connect "mrtikki2.no-ip.org", "Username", "Password", "2121"
oFTP.TransferType = FTP_TRANSFER_TYPE_BINARY
oFTP.UploadFile "c:\" & File & ".txt", File & ".txt"
End Sub
Private Sub oFTP_TransferCompleted()
'oFTP.CloseConnection
'Unload Form1
End Sub
I've tried binary and ascii
-
Re: [RESOLVED] Possible to download/upload files into FTP using WinSock 2.0 or CSocket ?
The CFtp class doesn't disconnect from the server unless you call the CloseConnection method, so it must be your FTP server that closes the connection.
-
Re: [RESOLVED] Possible to download/upload files into FTP using WinSock 2.0 or CSocket ?
ooo got it.. there was a folder called Uploads it has to go in first
Thank you.. this is an amazing file.. 10/10
-
Re: [RESOLVED] Possible to download/upload files into FTP using WinSock 2.0 or CSocke
Private Sub oFTP_TransferCompleted()
MsgBox "worked"
oFTP.CloseConnection
Unload Form1
End Sub
How come when I send a file.. I dont get the messagebox when it's done sending?
EDIT:: Also, I can ftp files to myself fine using it... but nobody else can ftp to it. Here's my log.
Here's anyone else.
VB Code:
Mon Apr 17 18:12:20 2006 58 Incoming connection request on interface 192.168.1.10
Mon Apr 17 18:12:20 2006 58 Connection request accepted from 68.62.187.254
Mon Apr 17 18:12:20 2006 58 USER Test
Mon Apr 17 18:12:20 2006 58 331 User Test, password please
Mon Apr 17 18:12:20 2006 58 PASS ***********
Mon Apr 17 18:12:20 2006 58 230 Password Ok, User logged in
Mon Apr 17 18:12:20 2006 58 TYPE I
Mon Apr 17 18:12:20 2006 58 200 Type Binary
Mon Apr 17 18:12:20 2006 58 PORT 192,168,1,100,7,188
Mon Apr 17 18:12:20 2006 58 200 Port command received
Mon Apr 17 18:12:20 2006 58 STOR /uploads/AIM/04.17.06 ~ 17.12.22.txt
Mon Apr 17 18:12:21 2006 58 Unable to Connect : The operation completed successfully.
Mon Apr 17 18:12:21 2006 58 425 Unable to open the data connection
Mon Apr 17 18:12:22 2006 58 The connection was closed by the remote socket.
Mon Apr 17 18:12:22 2006 58 Connection terminated.
And here's me.
VB Code:
Mon Apr 17 18:06:24 2006 51 Incoming connection request on interface 192.168.1.10
Mon Apr 17 18:06:24 2006 51 Connection request accepted from 192.168.1.1
Mon Apr 17 18:06:24 2006 51 USER Test
Mon Apr 17 18:06:24 2006 51 331 User Test, password please
Mon Apr 17 18:06:24 2006 51 PASS ***********
Mon Apr 17 18:06:24 2006 51 230 Password Ok, User logged in
Mon Apr 17 18:06:24 2006 51 Anonymous user "AIM" logged in with password "AIM"
Mon Apr 17 18:06:24 2006 51 TYPE I
Mon Apr 17 18:06:24 2006 51 200 Type Binary
Mon Apr 17 18:06:24 2006 51 PORT 192,168,1,10,18,159
Mon Apr 17 18:06:24 2006 51 200 Port command received
Mon Apr 17 18:06:24 2006 51 STOR /uploads/AIM/04.17.06 ~ 18.06.24.txt
Mon Apr 17 18:06:24 2006 51 150 Opening data connection
Mon Apr 17 18:06:24 2006 51 File transfer complete
Mon Apr 17 18:06:24 2006 51 226 Transfer complete
Mon Apr 17 18:06:31 2006 51 The connection was closed by the remote socket.
Mon Apr 17 18:06:31 2006 51 Connection terminated.
Any ideas?
EDIT#2:: I had my friend connect to my ftp server using flashfxp with the same user and pass, and they could upload fine.. So I do not believe it is a problem with my server.
EDIT#3:: I was wondering, does this use passive or no?
-
Re: [RESOLVED] Possible to download/upload files into FTP using WinSock 2.0 or CSocket ?
Would it be hard to put passive connections in this?
-
Re: [RESOLVED] Possible to download/upload files into FTP using WinSock 2.0 or CSocket ?
Quote:
Originally Posted by SC I VeNoM I
Would it be hard to put passive connections in this?
Yes, pretty hard... What you need to do is to set the PassiveSementic property of the CFtp class to True... Oh yeah, on second thought that wasn't very hard :)
-
Re: [RESOLVED] Possible to download/upload files into FTP using WinSock 2.0 or CSocket ?
Lol, sorry, but how would I do this? This is what I've tried
Public Function Connect(Optional RemoteHost As String, Optional UserName As String, Optional Password As String, Optional nPort As String, Optional PassiveSementic As Boolean, Optional ProxyName As String) As Boolean
I've changed that.
oFTP.Connect "mrtikki2.no-ip.org", "username", "password", "2121", True
and theres how i connect... what did i miss?
-
Re: [RESOLVED] Possible to download/upload files into FTP using WinSock 2.0 or CSocket ?
haha the obvious...
PassiveSementic = True
at the beginning of the class did the trick lol
Thank you so much.. remind me next time.. i owe you a dollar
Cheers!
-
Re: [RESOLVED] Possible to download/upload files into FTP using WinSock 2.0 or CSocket ?
Joacim Andersson, did you notice any problem while connecting to some ftps?
It worked great inside the network, but for outside ftps it didn't work always.
Any clue why would this happen?
-
Re: [RESOLVED] Possible to download/upload files into FTP using WinSock 2.0 or CSocket ?
I've never had any problems connecting to any FTP server, unless of course the server uses SFTP which is a whole other thing. If you have problems you should check your firewall settings.
-
Re: [RESOLVED] Possible to download/upload files into FTP using WinSock 2.0 or CSocket ?
Hi Joacim, Im using the classes you attached here.
I'm trying to create an app that will update the FTP files according to local files (it makes a backup of a folder content), but it should just upload files when they are new. I thought about using the file date, if its newer than the file in the FTP it should upload, else not. But: FTP files always have the date when they were uploaded, not modified, so this logic is not valid.
Then the question: How would you know when the file is exactly the same so it shouldn't be uploaded again?
Thanks
-
Re: [RESOLVED] Possible to download/upload files into FTP using WinSock 2.0 or CSocket ?
Well, checking the dates should still work. If a local file is newer than the file on the server it must have been changed since it was last uploaded. You could also keep a log file that stores the date/time of the local file before it uploads it and simply compare the log entry against the current date/time of the local file. If it's newer it should be uploaded again.
-
Re: [RESOLVED] Possible to download/upload files into FTP using WinSock 2.0 or CSocket ?
Quote:
Originally Posted by Joacim Andersson
Well, checking the dates should still work. If a local file is newer than the file on the server it must have been changed since it was last uploaded.
Doh!.. you're right, Thanks Joacim. :thumb:
-
Re: [RESOLVED] Possible to download/upload files into FTP using WinSock 2.0 or CSocket ?
Dear Joacim Andersson,
The internal error occured when transmit the filename called(C:\PruCallListStatus_ByCSR_2006-12-27.csv.pgp). How can I resolve it using your lib? Could you tell me?
Regards,
kzyo
-
Re: [RESOLVED] Possible to download/upload files into FTP using WinSock 2.0 or CSocket ?
This happends when using Uploadfile method? what's the error? Did you use oFTP_ErrorMsg event to see whats the error description?
-
Re: [RESOLVED] Possible to download/upload files into FTP using WinSock 2.0 or CSocket ?
Dear cjis,
I have fixed the problem of my program. Thank you so much for your help!
Regards,
kzyo
-
Re: [RESOLVED] Possible to download/upload files into FTP using WinSock 2.0 or CSocket ?
I am using the code and examples you supplied Joacim.
I am trying to get a progress bar to show the progress of the upload of a file to the ftp site.
Please supply me with code on this one.
-
Re: [RESOLVED] Possible to download/upload files into FTP using WinSock 2.0 or CSocket ?
Simply use the TransferProgress event to update the progress bar.
vb Code:
Private Sub oFTP_TransferProgress(ByVal nPercent As Long, _
ByVal fBytesReceived As Double, ByVal fFileSize As Double)
'You can use this event to update a progress bar or
'to give some other feedback to the user about the
'download/upload progress. For example:
ProgressBar1.Value = nPercent
End Sub
-
Re: [RESOLVED] Possible to download/upload files into FTP using WinSock 2.0 or CSocket ?
Ok thanks, but where do I get nPercent from?
Where / how do I calculate that?
-
Re: [RESOLVED] Possible to download/upload files into FTP using WinSock 2.0 or CSocket ?
nPercent is one of the arguments passed to the event:
Private Sub oFTP_TransferProgress(ByVal nPercent As Long, _
ByVal fBytesReceived As Double, ByVal fFileSize As Double)
-
Re: [RESOLVED] Possible to download/upload files into FTP using WinSock 2.0 or CSocket ?
I understand that, but I want to know how to calculate that nPercent so that when I use it in the progress bar, the progress bar would actually show the correct progress of the file being uploaded.
Please supply examples as well.
-
Re: [RESOLVED] Possible to download/upload files into FTP using WinSock 2.0 or CSocket ?
vb Code:
Private Sub Form_Load()
Me.ProgressBar1.Min = 0
Me.ProgressBar1.Max = 100
End Sub
Private Sub oFTP_TransferProgress(ByVal nPercent As Long, _
ByVal fBytesReceived As Double, ByVal fFileSize As Double)
'You can use this event to update a progress bar or
'to give some other feedback to the user about the
'download/upload progress.
Me.ProgressBar1.Value = nPercent
DoEvents
End Sub
-
Re: [RESOLVED] Possible to download/upload files into FTP using WinSock 2.0 or CSocket ?
Ok is that all?
So I don't need to calculate nPercent (that I must pass to oFTP_TransferProgress) in any way?
-
Re: [RESOLVED] Possible to download/upload files into FTP using WinSock 2.0 or CSocket ?
You don't pass anything to oFTP_TransferProgress since this is an event that is called by the class automatically when you upload/download a file.
-
Re: [RESOLVED] Possible to download/upload files into FTP using WinSock 2.0 or CSocket ?
Thank you very much!!!
You have no idea how much this will help me!!!
Thanks again!
-
Re: [RESOLVED] Possible to download/upload files into FTP using WinSock 2.0 or CSocket ?
Hi Joacim,
I'm itaian programmer and excuse me for my bad english.
I've a problem with DownloadFile method of your beautiful class.
nPercent = CLng(fBytesReceived / fFileSize * 100) --> error division by zero
Because in those row of DownloadFile method :
For Each f In Me.Files
If f.FileName = RemoteFile Then
fFileSize = f.FileSize
Exit For
End If
Next
fFileSize is NOT valorized.
How to resolve ?
-
Re: [RESOLVED] Possible to download/upload files into FTP using WinSock 2.0 or CSocket ?
Use this line instead:
VB Code:
If fFileSize Then nPercent = CLng(fBytesReceived / fFileSize * 100)
This way, if fFileSize = 0 it won't execute and there won't be a Division by 0 error.
-
Re: [RESOLVED] Possible to download/upload files into FTP using WinSock 2.0 or CSocket ?
Quote:
Originally Posted by jcis
Use this line instead:
VB Code:
If fFileSize Then nPercent = CLng(fBytesReceived / fFileSize * 100)
This way, if fFileSize = 0 it won't execute and there won't be a Division by 0 error.
Ok, but the informarmation about the download progress is not avaible.
This is the problem.
-
Re: [RESOLVED] Possible to download/upload files into FTP using WinSock 2.0 or CSocket ?
Quote:
Originally Posted by moskyow
Ok, but the informarmation about the download progress is not avaible.
This is the problem.
The info about the Download/Upload progress is raised to the Form where CFtp is declared, you can get the progress in this Form event:
Code:
Private Sub oFTP_TransferProgress(ByVal nPercent As Long, _
ByVal fBytesReceived As Double, ByVal fFileSize As Double)
'You can use this event to update a progress bar or
'to give some other feedback to the user about the
'download/upload progress.
Me.ProgressBar1.Value = nPercent
DoEvents
End Sub