2013-04-02 297 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