473,799 Members | 3,006 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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.LastInd exOf(",")
j = strTemp.LastInd exOf(")")
tPort = CInt(strTemp.Su bstring(i + 1, j - i - 1))
strTemp = strTemp.Substri ng(1, i - 1)
j = i
i = strTemp.LastInd exOf(",")
tPort = 256 * CInt(strTemp.Su bstring(i + 1, j - i - 2)) + tPort

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

MsgBox(tPort)

ReDim mBytes(FileLen( "C:\test.xl s"))

FileToUpload = File.OpenRead(" C:\test.xls")
q = FileToUpload.Re ad(mBytes, 0, FileLen("C:\tes t.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.Writ e(mBytes, k, intTmp)
FileMemory.Writ eTo(mDataStream )
k += intTmp
System.Console. Out.WriteLine(k )
Loop

mDataStream.Clo se()
uploadPort.Clos e()

Any help would be greatly appreciated.

Dec 13 '05 #1
4 1684
> 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.


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/

-

Dec 13 '05 #2
You may also wish to look at Secure FTP Factory for .NET ... it
includes components for both FTP and FTPS (FTP over SSL)

http://www.jscape.com/sftpdotnet/index.html

Dec 13 '05 #3
Hi,

Here is an example available on the microsoft site.

http://support.microsoft.com/default...b;en-us;832679

Ken
-------------------------
"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.

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.LastInd exOf(",")
j = strTemp.LastInd exOf(")")
tPort = CInt(strTemp.Su bstring(i + 1, j - i - 1))
strTemp = strTemp.Substri ng(1, i - 1)
j = i
i = strTemp.LastInd exOf(",")
tPort = 256 * CInt(strTemp.Su bstring(i + 1, j - i - 2)) + tPort

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

MsgBox(tPort)

ReDim mBytes(FileLen( "C:\test.xl s"))

FileToUpload = File.OpenRead(" C:\test.xls")
q = FileToUpload.Re ad(mBytes, 0, FileLen("C:\tes t.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.Writ e(mBytes, k, intTmp)
FileMemory.Writ eTo(mDataStream )
k += intTmp
System.Console. Out.WriteLine(k )
Loop

mDataStream.Clo se()
uploadPort.Clos e()

Any help would be greatly appreciated.

Dec 13 '05 #4

"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
Dec 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1528
by: Saurabh | last post by:
Hi all, I am using php3. For uploading a file i put <input type=file name=thesisfile> in the html file. now in the php3 file that i am using i used echo $thesisfile_type; echo $thesisfile_name; but it just does not work.
4
1504
by: ayanwen | last post by:
I have problem in uploading files using ASP, can you give me some sample code about uploading using ASP? -- ayanwen ------------------------------------------------------------------------ Posted via http://www.codecomments.com ------------------------------------------------------------------------
13
4324
by: Sky Sigal | last post by:
I have created an IHttpHandler that waits for uploads as attachments for a webmail interface, and saves it to a directory that is defined in config.xml. My question is the following: assuming that this is suppossed to end up as a component for others to use, and therefore I do NOT have access to their global.cs::Session_End() how do I cleanup files that were uploaded -- but obviously left stranded when the users aborted/gave up writting...
3
2365
by: Carlos | last post by:
Hello Forum, I would appreciate it if you could recommend settings to use auto-vacuum in my version 7.4 database. I am uploading several thousands records in the database at a rate of ~1 second per record (the data is uploading from a different server). I have noticed that the performance of the uploading is improved if I vacuum the database every so often so I decided to take advantage of auto-vacuuming to maintain the best...
7
2046
by: mantrid | last post by:
I have some code to upload files to my site. it works when the <input type="file" is posted once even when I use session variables from the posted variables but when I carry those session variables to a new page the upload will not work. despite the variables being set. I have echoed then on the second page and they display correctly but they just dont work in the move_uploaded_file() function. I get the error message Error uploading...
27
3863
by: jm | last post by:
I am having trouble understanding the purposes of an interface, even though the concept of interfaces is around me all the time (user interface, for example). I'm just not understanding software interfaces. Like anything else, it appears Interfaces are by design something that requires documentation. I know this may be obvious, but if I have a class A and I say, well, class A implements IMyInterface. The fact that it implements...
2
1679
by: ELINTPimp | last post by:
Hello all, Have a really interesting problem (at least to me) with my upload_file() function. I have it working now, with a bit of a work around, but would like to know what everyone thinks in regards to this being a bug or perhaps my ignorance. Basically the function takes two arguments, one is the desired file path I want the upload file to be moved to, and the second is an arbitrary integer. If the file doesn't already exist, the...
3
1917
ganesanji
by: ganesanji | last post by:
hi all, I have written a php coding for uploading a file to a specific folder or location in server which is a Linux server. I think the coding for file uploaing is correct. But it does not work properly. That means the file is not uploaded to the specified location. It always executes the "else" condition in my coding. I think the problem is lieing in the specifed path which is the target path for file uploading. I could not find out...
0
900
by: Anjana S | last post by:
hi.. i am creating a file transfer application to a remote location in vb.net...Are putfile and getfile inbuilt functions in vb.net just like connect(). if not how to implement uploading of files to a remote server???
0
10491
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10268
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10247
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10031
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9079
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6809
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4146
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2941
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.