FTP Visual Basic

Resulta que estoy haciendo una aplicación que genera unos reportes en formato HTML. Una vez creado el archivo, quiero publicarlo directamente en internet desde el Visual Basic. Para publicar utilizo WS_FTP.

1 respuesta

Respuesta
1
Puedes subir archivos a través de ftp... utilizando el componente Inet.
Te envio el código...
Option Explicit
Private msCurrentFile As String
Friend Sub FTPFile(ByVal sFTPServer As String, _
ByVal sFTPCommand As String, _
ByVal sFTPUser As String, _
ByVal sFTPPwd As String, _
ByVal sFTPSrcFileName As String, _
ByVal sFTPTgtFileName As String)
Dim oFS As Scripting.FileSystemObject
Dim sURL As String
On Error GoTo FTPFileExit
Me.HRG True
msCurrentFile = ""
Set oFS = New Scripting.FileSystemObject
sURL = "ftp://" & sFTPUser & ":" & sFTPPwd & "@"& sFTPServer
Inet1.Protocol = icFTP
Inet1.RequestTimeout = 60
Inet1.RemotePort = 21
Inet1.AccessType = icDirect
Inet1.URL = sURL
Select Case sFTPCommand
Case "PUT"
msCurrentFile = sFTPSrcFileName
If oFS.FileExists(sFTPSrcFileName) = False Then GoTo FTPFileExit
Inet1.Execute sURL, sFTPCommand & Space(1) & sFTPSrcFileName & " " & sFTPTgtFileName
Case "GET"
msCurrentFile = sFTPTgtFileName
If oFS.FileExists(sFTPTgtFileName) = True Then
oFS.DeleteFile sFTPTgtFileName, True
end if
Inet1.Execute sURL, sFTPCommand & Space(1) & sFTPSrcFileName & " " & sFTPTgtFileName
End Select
Me.WaitForResponse
Inet1.Execute sURL, "salir"
Me.WaitForResponse
FTPFileExit:
Set oFS = Nothing
HRG False
End Sub
Friend Sub WaitForResponse()
Dim fWait As Boolean
On Error GoTo ErrHandler
fWait = True
Do Until fWait = False
DoEvents
fWait = Inet1.StillExecuting
Loop
ErrHandler:
Err.Clear
End Sub
Private Sub Inet1_StateChanged(ByVal State As Integer)
On Error Resume Next
Select Case State
Case icNone
Case icResolvingHost: Me.lblRESPONSE.Caption = "Conectando servidor"
Case icHostResolved: Me.lblRESPONSE.Caption = "Servidor conectado"
Case icConnecting: Me.lblRESPONSE.Caption = "Conectando..."
Case icConnected: Me.lblRESPONSE.Caption = "Conectado"
Case icResponseReceived: Me.lblRESPONSE.Caption = "Enviando el Fichero..."
Case icDisconnecting: Me.lblRESPONSE.Caption = "Desconectando..."
Case icDisconnected: Me.lblRESPONSE.Caption = "Desconectado"
Case icError: MsgBox "Error:" & Inet1.ResponseCode & " " & Inet1.ResponseInfo
Case icResponseCompleted: Me.lblRESPONSE.Caption = "Proceso Completado."
End Select
Me.lblRESPONSE.Refresh
Err.Clear
End Sub
Friend Sub HRG(fShowHourGlass As Boolean)
If fShowHourGlass = True Then
Screen.MousePointer = 11
Else
Screen.MousePointer = 0
End If
End Sub
Private Sub Form_Unload(Cancel As Integer)
Set frmFTP = Nothing
End Sub
' Este es el codigo para FTPMain.bas
' Recuerda poner que arranque el proyecto en Sub Main()
Public Sub Main()
Load frmFTP
frmFTP.Show
FrmFTP.FTPFile "mi direccion ip", "PUT", "miusuario", "mipwd", "C:\temp\test.txt", "test.txt"
FrmFTP.FTPFile "mi direccion ip", "GET", "miusuario", "miwd", "test.txt", "c:\temp\test2.txt"
Unload frmFTP
End Sub
Se me olvidó comentarte que tienes que añadir también la referencia: microsoft scripting runtime.
que hace referencia a la scrrun.dll.
Salu2.
Muchísimas gracias, voy a probar con ese código a ver si me resulta. Si tengo alguna duda con respecto a su uso, te aviso. Espero poder devolverte el favor algún día.

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas