1

如何使用 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
4

1 回答 1

1

将 Internet Transfer Control 拖放到窗体上(VB6:如何添加 Inet 组件?)。然后使用它的Execute方法。请注意,无需将Protocol属性指定为ExecuteURL参数中得出的数字。

有关使用 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
于 2013-04-04T01:29:29.373 回答