Rubrik: HTML/Internet/Netzwerk · Internet / Browser / IE | VB-Versionen: VB4, VB5, VB6 | 23.04.03 |
FTP FileUpload via WININET.DLL Mal eben schnell eine Datei auf einen FTP-Server uploaden ;-) Wie das geht, erfahren Sie hier. | ||
Autor: Dieter Otter | Bewertung: | Views: 45.075 |
www.tools4vb.de | System: Win9x, WinNT, Win2k, WinXP, Win7, Win8, Win10, Win11 | Beispielprojekt auf CD |
Sicherlich kennen Sie bereits unsere Visual Basic Extended Library - sprich VBEx32.DLL - Extended VB Library v2.1.01. Hier finden Sie u.a. eine Funktion namens VBFILEUPLOAD, mit der sich eine beliebige Datei auf einen FTP-Server uploaden lässt.
Wie sich das ganze direkt in VB und mit Hilfe des Windows-API realisieren lässt, zeigt unser heutiger Tipp.
Fügen Sie nachfolgenden Code in ein Modul ein:
Option Explicit ' zunächst alle benötigten API-Deklarationen Private Declare Function InternetOpen Lib "wininet.dll" _ Alias "InternetOpenA" ( _ ByVal sAgent As String, _ ByVal nAccessType As Long, _ ByVal sProxyName As String, _ ByVal sProxyBypass As String, _ ByVal nFlags As Long) As Long Private Declare Function InternetCloseHandle Lib "wininet.dll" ( _ ByVal hInet As Long) As Integer Private Declare Function InternetConnect Lib "wininet.dll" _ Alias "InternetConnectA" ( _ ByVal hInternetSession As Long, _ ByVal sServerName As String, _ ByVal nServerPort As Integer, _ ByVal sUsername As String, _ ByVal sPassword As String, _ ByVal nService As Long, _ ByVal nFlags As Long, _ ByVal nContext As Long) As Long Private Declare Function FtpPutFile Lib "wininet.dll" _ Alias "FtpPutFileA" ( _ ByVal hFtpSession As Long, _ ByVal lpszLocalFile As String, _ ByVal lpszRemoteFile As String, _ ByVal dwFlags As Long, _ ByVal dwContext As Long) As Boolean ' Kostanten Private Const INTERNET_OPEN_TYPE_PRECONFIG = 0 Private Const INTERNET_INVALID_PORT_NUMBER = 0 Private Const INTERNET_SERVICE_FTP = 1 ' Übertragungsmodus Public Enum eTransferType FTP_TRANSFER_TYPE_BINARY = &H2 FTP_TRANSFER_TYPE_ASCII = &H1 End Enum ' Handles Private hOpen As Long Private hConnection As Long
' Verbindung zum Server herstellen Public Function Connect(ByVal sRemoteHost As String, _ Optional ByVal sUsername As String = "anonymous", _ Optional ByVal sPassword As String = "guest") As Boolean ' Ist noch eine Verbindung vorhanden? ' Wenn ja, muss diese zunächst beendet werden! If hOpen <> 0 Or hConnection <> 0 Then Disconnect End If ' Neue Verbindung starten hOpen = InternetOpen("FTP", _ INTERNET_OPEN_TYPE_PRECONFIG, vbNullString, _ vbNullString, 0) If hOpen Then hConnection = InternetConnect(hOpen, _ sRemoteHost, INTERNET_INVALID_PORT_NUMBER, _ sUsername, sPassword, INTERNET_SERVICE_FTP, 0, 0) End If Connect = (hConnection <> 0) End Function
' Datei auf den Server hochladen Public Function FileUpload( _ ByVal sLocalFilename As String, _ ByVal sRemoteFilename As String, _ Optional ByVal nTransferType As eTransferType = _ FTP_TRANSFER_TYPE_BINARY) As Boolean FileUpload = FtpPutFile(hConnection, _ sLocalFilename, sRemoteFilename, nTransferType, 0) End Function
' Verbindung zum Server beenden Public Sub Disconnect() If hConnection <> 0 Then InternetCloseHandle hConnection hConnection = 0 End If If hOpen <> 0 Then InternetCloseHandle hOpen hOpen = 0 End If End Sub
Aufrufbeispiel:
Dim sRemoteHost As String Dim sLocalFile As String Dim sRemoteFile As String Dim nTransferMode As eTransferType Dim bResult As Boolean ' Anmeldung am eigenen T-Online Server sRemoteHost = "home-up.t-online.de" If Connect(sRemoteHost) Then ' Lokaler Dateiname sLocalFile = App.Path & "\myHelp.txt" ' Ziel-Datei auf dem Server ' Achtung! Verzeichnis muss bereits existsieren!!! sRemoteFile = "/download/help/myHelp.txt" ' Ascii-Übertragung nTransferMode = FTP_TRANSFER_TYPE_ASCII ' Upload durchführen bResult = FileUpload(sLocalFile, sRemoteFile, nTransferMode) If bResult Then MsgBox "Upload erfolgreich!" Else MsgBox "Fehler beim Upload der Datei!" End If ' Verbindung trennen Disconnect End If
Anmerkung:
Die Datei wird in "einem Rutsch" hochgeladen. Während dieser Zeit erfolgt keine Statusmeldung - auch bekommt Ihr Programm erst nach erfolgtem Upload wieder die Kontrolle.