Question Need help with FTP

Meester69

Member
Joined
Sep 13, 2012
Messages
9
Programming Experience
1-3
I'm writing a basic program for a company to capture POD files.
I am unable to figure out how the code works to FTP a file or a batch of files. The files are text files created by the program.

I have a default server for them to go to

Here is my code
VB.NET:
Imports System.Data.OleDb
Public Class Form1
    Dim sw As IO.StreamWriter

    Private Property ftpServer As Object


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim dt As Date = DateTimePicker1.Text
        Dim tm As Date = DateTimePicker2.Text
        Dim fm1 As String = "yyyyMMdd"
        Dim fm2 As String = "HHmm"
        Dim Awb As String = TextBox1.Text
        Dim name As String = TextBox2.Text
        Dim value As Integer = NumericUpDown1.Text
        If Not System.IO.Directory.Exists("C:\POD") Then
            System.IO.Directory.CreateDirectory("C:\POD")
        End If
        If IO.File.Exists("C:\POD\" & "podupl." & TextBox1.Text & ".txt") = False Then
            sw = IO.File.CreateText("C:\POD\" & "podupl." & TextBox1.Text & ".txt")

            sw.WriteLine("POD Upload")

            sw.WriteLine(Awb & "|" & dt.ToString(fm1) & "|" & tm.ToString(fm2) & "|" & name & "|" & value)

            TextBox1.Clear()
            TextBox2.Clear()
            NumericUpDown1.Value = 0
            sw.WriteLine("END-OF-FILE")
            sw.Close()
            Label6.Text = "Captured successfully"

        ElseIf IO.File.Exists("C:\POD\" & "podupl." & TextBox1.Text & ".txt") = True Then
            Dim YesNo = MsgBox("Attention! POD already catured. Do you want to overwrite file?", vbYesNo)
            Select Case YesNo
                Case vbYes
                    sw = New IO.StreamWriter("C:\POD\" & "podupl." & TextBox1.Text & ".txt", False)
                    sw.WriteLine("POD Upload")

                    sw.WriteLine(Awb & "|" & dt.ToString(fm1) & "|" & tm.ToString(fm2) & "|" & name & "|" & value)

                    TextBox1.Clear()
                    TextBox2.Clear()
                    NumericUpDown1.Value = 0
                    Label6.Text = "Captured"
                    sw.WriteLine("END-OF-FILE")
                    sw.Close()
                    Label6.Text = "Overwrite successfull"
                Case vbNo
                    Exit Sub
            End Select
        End If
        Button1.Enabled = False
        NumericUpDown1.Focus()
    End Sub

    Private Sub TextBox1_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.Enter
        Label6.Text = ""
    End Sub

    Private Sub NumericUpDown1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NumericUpDown1.ValueChanged
        If TextBox1.Text = "" And TextBox2.Text = "" And NumericUpDown1.Value < 1 Then
            Button1.Enabled = False
        Else
            Button1.Enabled = True
        End If
    End Sub

    Private Sub TextBox1_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseClick
        Label6.Text = ""
    End Sub

    Private Sub ToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem1.Click

    End Sub
End Class
I'm going to be using a drop down menu giving the user a choice to FTP a batch or a single file...
Please help!!
 
Last edited:
To upload to FTP you can for example use the My.Computer.Network object and its UploadFile method, or the WebClient class which also has a UploadFile method.
 
I did some research about the WebClient but I'm not getting the concept. It is my first time working with this as we did not go this far in my studies.
 
Meester69 said:
about the WebClient but I'm not getting the concept
But the My.Computer.Network.UploadFile call turned out fine for you?
 
Here is my code:

VB.NET:
Imports System.Data.OleDb
Public Class Form1
    Dim sw As IO.StreamWriter

    Private Property ftpServer As Object

    Private Sub TextBox1_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.Enter
        Label6.Text = ""
    End Sub

    Private Sub NumericUpDown1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NumericUpDown1.ValueChanged
        If TextBox1.Text = "" And TextBox2.Text = "" And NumericUpDown1.Value < 1 Then
            CaptureAndSendToolStripMenuItem.Enabled = False
        Else
            CaptureAndSendToolStripMenuItem.Enabled = True
        End If
    End Sub

    Private Sub TextBox1_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseClick
        Label6.Text = ""
    End Sub

    Private Sub CaptureAndSendToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CaptureAndSendToolStripMenuItem.Click
        Dim dt As Date = DateTimePicker1.Text
        Dim tm As Date = DateTimePicker2.Text
        Dim fm1 As String = "yyyyMMdd"
        Dim fm2 As String = "HHmm"
        Dim Awb As String = TextBox1.Text
        Dim name As String = TextBox2.Text
        Dim value As Integer = NumericUpDown1.Text
        If Not System.IO.Directory.Exists("C:\POD") Then
            System.IO.Directory.CreateDirectory("C:\POD")
        End If
        If IO.File.Exists("C:\POD\" & "podupl." & TextBox1.Text & ".txt") = False Then
            sw = IO.File.CreateText("C:\POD\" & "podupl." & TextBox1.Text & ".txt")

            sw.WriteLine("POD Upload")

            sw.WriteLine(Awb & "|" & dt.ToString(fm1) & "|" & tm.ToString(fm2) & "|" & name & "|" & value)

            TextBox1.Clear()
            TextBox2.Clear()
            NumericUpDown1.Value = 0
            sw.WriteLine("END-OF-FILE")
            sw.Close()
            Label6.Text = "Captured successfully"

        ElseIf IO.File.Exists("C:\POD\" & "podupl." & TextBox1.Text & ".txt") = True Then
            Dim YesNo = MsgBox("Attention! POD already catured. Do you want to overwrite file?", vbYesNo)
            Select Case YesNo
                Case vbYes
                    sw = New IO.StreamWriter("C:\POD\" & "podupl." & TextBox1.Text & ".txt", False)
                    sw.WriteLine("POD Upload")

                    sw.WriteLine(Awb & "|" & dt.ToString(fm1) & "|" & tm.ToString(fm2) & "|" & name & "|" & value)

                    TextBox1.Clear()
                    TextBox2.Clear()
                    NumericUpDown1.Value = 0
                    Label6.Text = "Captured"
                    sw.WriteLine("END-OF-FILE")
                    sw.Close()
                    Label6.Text = "Overwrite successfull"
                Case vbNo
                    Exit Sub
            End Select
        End If
        Try
            My.Computer.Network.UploadFile("C:\POD\" & "podupl." & TextBox1.Text & ".txt", "ftp://<server>", "<username>", "<password>")
            MessageBox.Show("File uploaded.")
        Catch ex As Exception
            MessageBox.Show("Access failed" & vbCrLf & ex.Message)
        End Try
        CaptureAndSendToolStripMenuItem.Enabled = False
        NumericUpDown1.Focus()
    End Sub
End Class

For some reason, it not finding the file to FTP on my local directory.
 
You are using TextBox1.Text in file name, but before that you have called TextBox1.Clear(). Instead of creating the file path string again and again and again, why not just assign it to a variable and be done with it?
 
Ahh Such a simple mistake thanks JohnH.

It's giving me a different error now, it says "An exception occurred during a WebClient request."
 
The exception will tell what went wrong.
One thing to note is that you posted "ftp://<server>", that must be the full path to the file in ftp server, not just a folder.
 
I found out it was the Proxy blocking it, but it still gives me an error saying:
error.gif
I am using the full path
 
FTP code 227 is not an error, it is success confirmation (Positive Completion reply) about the passive FTP mode, passive is the default mode. Still, looking at web I see other reports about "error: 227". Possible causes can be server failing to accept the data channel connection, or client firewall blocking the new outgoing socket.
If it happens to be a congested server, it may be possible still to use active mode, but that would be a lot more difficult for you to program involving at least a custom WebClient and would also require client computer to accept incoming connections (through firewalls and routers if any). Though the 227 message clearly says the server supports passive mode, so I think it would be simpler for you to resolve that.
 

Latest posts

Back
Top