Reputation: 231
How would I upload a textfile to my ftp server using Visual Basic 6.0?
I want to upload "C:\hello.txt" to "files/hello.txt" on my server.
I've previously tried this code, without success:
Function UploadFile(ByVal HostName As String, _
ByVal UserName As String, _
ByVal Password As String, _
ByVal LocalFileName As String, _
ByVal RemoteFileName As String) As Boolean
Dim FTP As Inet
Set FTP = New Inet
With FTP
.Protocol = icFTP
.RemoteHost = HostName
.UserName = UserName
.Password = Password
.Execute .URL, "Put " + LocalFileName + " " + RemoteFileName
Do While .StillExecuting
DoEvents
Loop
UploadFile = (.ResponseCode = 0)
End With
Set FTP = Nothing
End Function
Upvotes: 1
Views: 13002
Reputation: 3220
Drop Internet Transfer Control on the form (VB6: how to add Inet component?). Then use its Execute
method. Note that there's no need to specify Protocol
property as Execute
figures it out from URL
argument.
There is MSDN walkthrough on using Internet Transfer Control: http://msdn.microsoft.com/en-us/library/aa733648%28v=vs.60%29.aspx
Option Explicit
Private Declare Sub Sleep Lib "kernel32.dll" _
(ByVal dwMilliseconds As Long)
Private Function UploadFile(ByVal sURL As String _
, ByVal sUserName As String _
, ByVal sPassword As String _
, ByVal sLocalFileName As String _
, ByVal sRemoteFileName As String) As Boolean
'Pessimist
UploadFile = False
With Inet1
.UserName = sUserName
.Password = sPassword
.Execute sURL, "PUT " & sLocalFileName & " " & sRemoteFileName
'Mayhaps, a better idea would be to implement
'StateChanged event handler
Do While .StillExecuting
Sleep 100
DoEvents
Loop
UploadFile = (.ResponseCode = 0)
Debug.Print .ResponseCode
End With
End Function
Private Sub cmdUpload_Click()
UploadFile "ftp://localhost", "", "", "C:\Test.txt", "/Level1/Uploaded.txt"
End Sub
Upvotes: 1