tutlogger

Create an FTP Server Application on VB.Net




The commonly used method most people know to transfer or share files over the internet, which most websites use, is through HTTP or (Hyper Text Transfer Protocol). If your are doing the same method, I am here to suggest an alternative, and that is to use a special protocol called FTP or (File Transfer Protocol). It is a protocol designed for the process of transferring files from one computer to another over the Internet without interruptions. You can use this method when you want to upload or download files from remote locations in your computer anywhere you are at any time. But be sure to have an internet connection from your server before you could do the process.

In this tutorial, I will discuss how to create an FTP Server Application on VB.Net. It will include an upload and download file.You can also view your actual FTP server window in a web browser component and be able to delete or rename files.

Here are the steps to make an FTP Server Application.

Step 1. Download  FileZilla Server Application and install it on your computer.





Step 2. Setup your FileZilla FTP Server. If you have no idea on how to do it, watch this video I found on YouTube.




Step 3. After setting up your FTP Server, create a new VB project on Visual Studio and make a form design like this.




Step 4. I will not discuss all the codes involved in this project application. I will concentrate on how to LOAD, UPLOAD and DOWNLOAD file/s only. Don't worry because I have provided a download link of the complete working project at the end of this tutorial. Now let's see the codes.


*LOAD File/s from FTP Server

 Private Sub btn_load_Click(sender As Object, e As EventArgs) Handles btn_load.Click
        Try
            Me.Cursor = Cursors.WaitCursor
            If txtserveraddress.Text = Nothing Then
                MessageBox.Show("Enter FTP Server address.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information)
                txtserveraddress.Focus()
            ElseIf txtserverusername.Text = Nothing Then
                MessageBox.Show("Enter FTP Server username", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information)
                txtserverusername.Focus()
            ElseIf txtserverpassword.Text = Nothing Then
                MessageBox.Show("Enter FTP Server password", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information)
                txtserverpassword.Focus()
            Else
                'Create an FTP web request
                Dim ftpwebrequest As FtpWebRequest = DirectCast(WebRequest.Create("ftp://" & txtserveraddress.Text & ":21/"), FtpWebRequest)
                'Set properties
                With ftpwebrequest
                    'ftp server username and password
                    .Credentials = New NetworkCredential(txtserverusername.Text, txtserverpassword.Text)
                    'set the method to download
                    .Method = WebRequestMethods.Ftp.ListDirectory
                    'upload timeout to 100 seconds
                    .Timeout = "100000"
                End With

                Dim ftpwebres As FtpWebResponse = CType(ftpwebrequest.GetResponse(), FtpWebResponse)
                Dim ftpstreamreader As StreamReader = New StreamReader(ftpwebres.GetResponseStream())
                'clear list of files
                lstfiles.Items.Clear()
                'start loading files from an FTP server into list
                While (ftpstreamreader.Peek() > -1)
                    lstfiles.Items.Add(ftpstreamreader.ReadLine())
                End While
            End If
            Me.Cursor = Cursors.Default
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End Try

    End Sub

*UPLOAD a File to FTP Server

   Private Sub btn_upload_Click(sender As Object, e As EventArgs) Handles btn_upload.Click
        Try
            Me.Cursor = Cursors.WaitCursor
            If txtserveraddress.Text = Nothing Then
                MessageBox.Show("Enter FTP Server address.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information)
                txtserveraddress.Focus()
            ElseIf txtserverusername.Text = Nothing Then
                MessageBox.Show("Enter FTP Server username", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information)
                txtserverusername.Focus()
            ElseIf txtserverpassword.Text = Nothing Then
                MessageBox.Show("Enter FTP Server password", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information)
                txtserverpassword.Focus()
            Else
                'Create an FTP web request
                Dim ftpwebrequest As FtpWebRequest = DirectCast(WebRequest.Create("ftp://" & txtserveraddress.Text & ":21/" & get_filesafename.ToString), FtpWebRequest)
                'Set properties
                With ftpwebrequest
                    .Credentials = New NetworkCredential(txtserverusername.Text, txtserverpassword.Text)
                    'set the method to upload
                    .Method = WebRequestMethods.Ftp.UploadFile
                    'upload timeout to 100 seconds
                    .Timeout = "100000"
                    'data transfer type
                    .UseBinary = True
                    'size of the file to upload
                    .ContentLength = get_filename.Length
                End With

                Dim ufile() As Byte = File.ReadAllBytes(get_filename.ToString)

                Dim ftpwebstreamrequest As Stream = ftpwebrequest.GetRequestStream()
                'start streaming upload
                ftpwebstreamrequest.Write(ufile, 0, ufile.Length)
                'close and dispose file and request stream after the upload
                ftpwebstreamrequest.Close()
                ftpwebstreamrequest.Dispose()
                'refresh web browser list
                tsbRefresh.PerformClick()
                Me.Cursor = Cursors.Default
                MessageBox.Show("Upload completed", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End Try
    End Sub



*DOWNLOAD a File from FTP Server


 Private Sub btn_download_Click(sender As Object, e As EventArgs) Handles btn_download.Click
        Try
            Me.Cursor = Cursors.WaitCursor
            Dim savefildlg As SaveFileDialog = New SaveFileDialog
            If txtserveraddress.Text = Nothing Then
                MessageBox.Show("Enter FTP Server address.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information)
                txtserveraddress.Focus()
            ElseIf txtserverusername.Text = Nothing Then
                MessageBox.Show("Enter FTP Server username", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information)
                txtserverusername.Focus()
            ElseIf txtserverpassword.Text = Nothing Then
                MessageBox.Show("Enter FTP Server password", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information)
                txtserverpassword.Focus()
            Else
                'Create a FTP web request
                Dim ftpwebrequest As FtpWebRequest = DirectCast(WebRequest.Create("ftp://" & txtserveraddress.Text & ":21/" & txtdownloadfile.Text), FtpWebRequest)
                'Set properties
                With ftpwebrequest
                    'ftp server username and password
                    .Credentials = New NetworkCredential(txtserverusername.Text, txtserverpassword.Text)
                    'set the method to download
                    .Method = WebRequestMethods.Ftp.DownloadFile
                    'upload timeout to 100 seconds
                    .Timeout = "100000"
                End With

                Dim ftpwebres As FtpWebResponse = CType(ftpwebrequest.GetResponse(), FtpWebResponse)
                Dim ftpstream As Stream = ftpwebres.GetResponseStream()

                With savefildlg
                    .Title = "Save File Download from FTP Server"
                    .FileName = Path.GetFullPath(ftpwebrequest.RequestUri.LocalPath)
                    'set type of files
                    .Filter = "File Formats(*.jpg;*.jpeg;*.txt;*.doc;*.docx;*.zip;*.rar)|*.jpg;*.jpeg;*.txt;*.doc;*.docx;*.zip;*.rar|Image File(*.jpg;*.jpeg)|*.jpg;*.jpeg|Text File(*.txt)|*.txt|Documents(*.dic;*.docx)|*.doc;*.docx|Compressed Files(*.zip;*.rar)|*.zip;*.rar"
                End With

                'start streaming download
                If savefildlg.ShowDialog = Windows.Forms.DialogResult.OK Then
                    Dim ftpfilestream As FileStream = File.Create(savefildlg.FileName)
                    Dim buff(1024) As Byte
                    Dim bytesread As Integer = 0
                    While True
                        bytesread = ftpstream.Read(buff, 0, buff.Length)
                        If (bytesread = 0) Then Exit While
                        ftpfilestream.Write(buff, 0, bytesread)
                    End While
                End If
                Me.Cursor = Cursors.Default
                MessageBox.Show("Download completed", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End Try
    End Sub

***NOTE: In this line of code "ftp://" & txtserveraddress.Text & ":21/", the number 21 in color red is the default port number of FileZilla FTP Server. You can find that value under Edit > Settings >> General Settings.

Try to make an application like this if you consider it useful. 

Download complete working project below. 

IDE: Visual Studio 2012
Project Type: Visual Basic Windows
Database: No Database






SHARE THIS POST

2 comments:

  1. An Indian Restaurant & Takeaway in Croydon. We serve a wide range of delicious Asian & Indian food. We offer online ordering and table booking.For reservation call 02036678566


    adeenaskitchen.co.uk/

    ReplyDelete

PRIVACY POLICY | TERMS OF SERVICE | COPYRIGHT POLICY