2013-04-02 264 views
1

如何使用Visual Basic 6.0将文本文件上传到我的ftp服务器?通过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 
+1

又是怎么回事Ftp.url?您从未向您展示过的样品中分配过它。 “没有成功”是什么意思? –

+1

结果如何?你如何调用UploadFile? (你将LocalFileName和RemoteFilename设置为什么?你在LocalFileName中包含路径.ResponseCode的值是什么? – Hrqls

回答

1

降形式(VB6: how to add Inet component?)在互联网上传输控制。然后使用其Execute方法。请注意,没有必要指定Protocol属性,因为ExecuteURL参数中计算出来。

有使用互联网传输控制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