"XxLicherxX " <go********@aol .com> wrote in message
news:11******** **************@ g43g2000cwa.goo glegroups.com.. .
Hello everyone,
I have created an application that generates some files. What I want
the application to do now is to automatically FTP these files to a
webserver. I have started implementing an FTP client into my code. I
can log into my FTP Server, PWD, CWD, etc just fine.
If you are still having trouble, I have just had to do exactly the same
(i.e. upload files).
The code I use is below - you'll need to fiddle around with it for your
needs but you should get the idea.
James
Public Class FTPClient
Public IsConnected As Boolean = False
Public IsStreamReady As Boolean = False
Public IPFound As Boolean = False
Public Username As String = "your FTP username"
Public Password As String = "your FTP Password"
Public FTPHost As String = "your FTP server IP"
Public PortNo As Integer = 21
Public TCPCli As TcpClient
Public NetStream As NetworkStream
Public StreamRd As StreamReader
Delegate Sub SendProgress(By Val o As Object, ByVal e As ByteEventArgs)
Public Event TByteEvent As SendProgress
Public CancelFlag As Boolean = False
Public PracDesc As String
Public NowDate As String
Public RemPath As String
Public Sub OpenConn()
Dim StrOut As String
TCPCli = New TcpClient(FTPHo st, PortNo)
TCPCli.ReceiveB ufferSize = 4096
NetStream = TCPCli.GetStrea m
StreamRd = New StreamReader(Ne tStream)
StrOut = StreamRd.ReadLi ne
WriteToStream(" USER " & Username & vbCrLf)
StrOut = StrOut & StreamRd.ReadLi ne
WriteToStream(" pass " & Password & vbCrLf)
StrOut = StrOut & StreamRd.ReadLi ne
If InStr(StrOut, "230") Then
'logged in
IsConnected = True
ElseIf InStr(StrOut, "530") Then
'login failed
IsConnected = False
End If
End Sub
Public Sub CloseConn()
WriteToStream(" bye" & vbCrLf)
NetStream.Close ()
StreamRd = Nothing
TCPCli.Close()
End Sub
Private Sub WriteToStream(B yVal Command As String)
NetStream.Write (System.Text.En coding.ASCII.Ge tBytes(Command & vbCrLf),
0, Command.Length + 1)
End Sub
Public Function SendFile(ByVal sLocal As String, ByVal sRemote As String) As
String
'Vars
Dim IPAddr As IPHostEntry
Dim RandNum As Random
Dim FStream As FileStream
Dim LPort As Integer = 0
Dim Conn As TcpClient
Dim bData(1024) As Byte
Dim BytesRead As Integer
Dim xFer As Socket
Dim TotalBytes As Long
'Create Filestream for the local file read.
FStream = New FileStream(sLoc al, FileMode.Open, FileAccess.Read ,
FileShare.Read, 1024, False)
Dim sOut As String
'Enter passive mode
WriteToStream(" PASV" & vbCrLf)
Application.DoE vents()
sOut = StreamRd.ReadLi ne
Dim StrSplit() As String
Dim RemIP As String
Dim RemPort As Integer
If Mid(sOut, 1, 3) = "227" Then
sOut = Mid(sOut, InStr(sOut, "(") + 1, (InStr(sOut, ")")) -
(InStr(sOut, "(") + 1))
StrSplit = Split(sOut, ",")
RemIP = (StrSplit(0) & "." & StrSplit(1) & "." & StrSplit(2) & "." &
StrSplit(3))
RemPort = CInt(StrSplit(4 )) * 256 + StrSplit(5)
IPFound = True
End If
If IPFound = False Then Exit Function
NowDate = Now
NowDate = Replace(NowDate , "-", "")
NowDate = Replace(NowDate , ":", "")
NowDate = Replace(NowDate , "/", "")
'Create folder for upload
RemPath = PracDesc & "-" & NowDate
WriteToStream(" mkd " & PracDesc & "-" & NowDate & vbCrLf)
Application.DoE vents()
sOut = StreamRd.ReadLi ne
'Change directory
WriteToStream(" cwd " & PracDesc & "-" & NowDate & vbCrLf)
Application.DoE vents()
sOut = StreamRd.ReadLi ne
'Set binary transfer mode
WriteToStream(" TYPE i" & vbCrLf)
Application.DoE vents()
sOut = StreamRd.ReadLi ne
'Create datastream connection
Try
Conn = New TcpClient(RemIP , RemPort)
Catch ex As Exception
'Error connecting.
MsgBox("Data connection could not be established:" & vbCrLf & vbCrLf
& ex.GetBaseExcep tion.Message, MsgBoxStyle.Exc lamation, "Error")
SendFile = "Data connection could not be estabilshed"
Exit Function
End Try
'send command to upload the file
WriteToStream(" stor " & Replace(Path.Ge tFileName(sLoca l), ".copied.md b",
"") & vbCrLf)
sOut = StreamRd.ReadLi ne
'Begin upload of data
Dim TheStream As Stream = Conn.GetStream
Dim StreamStatus As Boolean = TheStream.CanWr ite
BytesRead = FStream.Read(bD ata, 0, 1024)
TheStream.Write (bData, 0, 1024)
TotalBytes = 1024
While BytesRead > 0
If CancelFlag = True Then
Exit While
End If
Try
BytesRead = FStream.Read(bD ata, 0, 1024)
TheStream.Write (bData, 0, BytesRead)
TotalBytes = TotalBytes + 1024
RaiseEvent TByteEvent(Me, New ByteEventArgs(T otalBytes))
Catch ex As Exception
If CancelFlag = True Then
Exit While
Else
MsgBox("Error sending data:" & vbCrLf & vbCrLf &
ex.GetBaseExcep tion.Message & vbCrLf & vbCrLf & "Stack Trace:" & vbCrLf &
ex.GetBaseExcep tion.StackTrace , MsgBoxStyle.Exc lamation, "Error sending
data")
Exit While
End If
End Try
End While
FStream.Close()
TheStream.Close ()
Conn.Close()
Conn = Nothing
xFer = Nothing
FStream = Nothing
TheStream = Nothing
sOut = StreamRd.ReadLi ne
'return string.
SendFile = sOut
End Function
End Class