Trying to implement FTP, can't get uploading to work

X

XxLicherxX

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.

The problem starts when I try to upload a file. The part of the code
that is supposed to upload the file will run without any errors, but
when I login to my FTP site, the file I just tried to upload is not
there.

Below is the code snippet I am using (I found it online, and changed it
a tad bit) I think I understand most of it, but obviously not
everything, otherwise it would be working. Can someone tell me where I
am going wrong?

strTemp = responseData 'Response data is the result I get
after sending the
PASV command

'This looks like it somehow calculates the port that will be used for
transferring data. I have no idea how this formula works.

i = strTemp.LastIndexOf(",")
j = strTemp.LastIndexOf(")")
tPort = CInt(strTemp.Substring(i + 1, j - i - 1))
strTemp = strTemp.Substring(1, i - 1)
j = i
i = strTemp.LastIndexOf(",")
tPort = 256 * CInt(strTemp.Substring(i + 1, j - i - 2)) + tPort

'Client we will use for uploading the files to the webserver.
Dim uploadPort As New TcpClient(server, tPort)
Dim mDataStream As NetworkStream = uploadPort.GetStream()

MsgBox(tPort)

ReDim mBytes(FileLen("C:\test.xls"))

FileToUpload = File.OpenRead("C:\test.xls")
q = FileToUpload.Read(mBytes, 0, FileLen("C:\test.xls"))

'Trying to upload here, not sure what the 16384 refers to.

intTmp = 16384
Do While k < mBytes.Length
If mBytes.Length - k < 16384 Then
intTmp = mBytes.Length - k
End If
FileMemory.Write(mBytes, k, intTmp)
FileMemory.WriteTo(mDataStream)
k += intTmp
System.Console.Out.WriteLine(k)
Loop

mDataStream.Close()
uploadPort.Close()

Any help would be greatly appreciated.
 
L

Lance R.

The problem starts when I try to upload a file. The part of the code
is supposed to upload the file will run without any errors, but
when I login
to my FTP site, the file I just tried to upload is not

Does the
tcp connection for uploading the data get successfully created? You can sniff
this to be sure. Also, are you sending the command to start the upload in the
first place?

You might try a third party component, there are tons available,
like the one from /n software:
http://www.nsoftware.com/products/component/ftp.aspx.

Regards,
Lance R.
/n
software
http://www.nsoftware.com/

-
 
J

JamesB

XxLicherxX said:
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(ByVal 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(FTPHost, PortNo)
TCPCli.ReceiveBufferSize = 4096
NetStream = TCPCli.GetStream
StreamRd = New StreamReader(NetStream)
StrOut = StreamRd.ReadLine
WriteToStream("USER " & Username & vbCrLf)
StrOut = StrOut & StreamRd.ReadLine
WriteToStream("pass " & Password & vbCrLf)
StrOut = StrOut & StreamRd.ReadLine
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(ByVal Command As String)
NetStream.Write(System.Text.Encoding.ASCII.GetBytes(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(sLocal, FileMode.Open, FileAccess.Read,
FileShare.Read, 1024, False)
Dim sOut As String

'Enter passive mode
WriteToStream("PASV" & vbCrLf)
Application.DoEvents()
sOut = StreamRd.ReadLine
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.DoEvents()
sOut = StreamRd.ReadLine

'Change directory
WriteToStream("cwd " & PracDesc & "-" & NowDate & vbCrLf)
Application.DoEvents()
sOut = StreamRd.ReadLine

'Set binary transfer mode
WriteToStream("TYPE i" & vbCrLf)
Application.DoEvents()
sOut = StreamRd.ReadLine

'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.GetBaseException.Message, MsgBoxStyle.Exclamation, "Error")
SendFile = "Data connection could not be estabilshed"
Exit Function
End Try

'send command to upload the file
WriteToStream("stor " & Replace(Path.GetFileName(sLocal), ".copied.mdb",
"") & vbCrLf)
sOut = StreamRd.ReadLine

'Begin upload of data
Dim TheStream As Stream = Conn.GetStream
Dim StreamStatus As Boolean = TheStream.CanWrite
BytesRead = FStream.Read(bData, 0, 1024)
TheStream.Write(bData, 0, 1024)
TotalBytes = 1024
While BytesRead > 0
If CancelFlag = True Then
Exit While
End If
Try
BytesRead = FStream.Read(bData, 0, 1024)
TheStream.Write(bData, 0, BytesRead)
TotalBytes = TotalBytes + 1024
RaiseEvent TByteEvent(Me, New ByteEventArgs(TotalBytes))
Catch ex As Exception
If CancelFlag = True Then
Exit While
Else
MsgBox("Error sending data:" & vbCrLf & vbCrLf &
ex.GetBaseException.Message & vbCrLf & vbCrLf & "Stack Trace:" & vbCrLf &
ex.GetBaseException.StackTrace, MsgBoxStyle.Exclamation, "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.ReadLine

'return string.
SendFile = sOut
End Function

End Class
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top