2013-04-02 5 views
1

Comment télécharger un fichier texte sur mon serveur FTP à l'aide de Visual Basic 6.0?Télécharger un fichier via ftp

Je veux télécharger "C: \ hello.txt" sur "files/hello.txt" sur mon serveur.

Je l'ai déjà essayé ce code, sans succès:

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

Qu'en est-Ftp.url? Vous ne l'avez jamais assigné dans un échantillon que vous nous montrez. Et qu'entendez-vous par "sans succès"? –

+1

Quel est le résultat? Comment appelez-vous UploadFile? (Que définissez-vous LocalFileName et RemoteFilename? Avez-vous inclus le chemin dans LocalFileName Quelle est la valeur de .ResponseCode? – Hrqls

Répondre

1

Drop Control Internet Transfer sur le formulaire (VB6: how to add Inet component?). Ensuite, utilisez sa méthode Execute. Notez qu'il n'est pas nécessaire de spécifier la propriété Protocol comme Execute figure à partir de URL argument.

Il est soluce MSDN sur l'utilisation Transfer Control Internet: 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 
Questions connexes