如何使用Visual Basic 6.0将文本文件上传到我的ftp服务器?
我想在我的服务器上将“C:\ hello.txt”上传到“files / hello.txt”。
我之前尝试过这段代码但没有成功:
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
答案 0 :(得分:1)
删除表单上的Internet传输控制(VB6: how to add Inet component?)。然后使用其Execute
方法。请注意,无需指定Protocol
属性,因为Execute
从URL
参数中找出它。
有关使用Internet传输控制的MSDN演练: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