Home > Visual Basic > Writing files to the Internet via FTP (Part, the first)

Writing files to the Internet via FTP (Part, the first)

Hello, all.  Today’s post is more of a tutorial. Today I’m going to teach you how to write an xml file to the internet using FTP Services.

Requirements: A webhost (such as 000webhost.com: make sure it is already registered and set up with a domain name. You can get a domain name from http://www.co.cc),  Filezilla (optional, but recommend), Today’s source code (optional), and of course, VB.NET 2010.

The key is the System.Net Namespace.  This namespace, when properly used, will help us utilize the internet by uploading and dowloading files.  Today, we are going to upload an XML file to the internet via FTP.

Get your FTP information

First, you need to get your:

  • Host name (example: yourwebsite.co.cc)
  • FTP username (example: a3141519)
  • FTP password (example: nohackers)

If you are using 000webhost, here’s how to get this information.

  1. Go to 000webhost.com.
  2. Click “Members Area”; it’s on the right side.
  3. Log in.
  4. In the blue table, in the >>Action column, click “Go to cPanel”
  5. In the “>>Files” section, click “FTP details”.
  6. Save this information or bookmark this page.  We’ll need this stuff later.

If you are not using 000webhost, then you’re on your own. Sorry.

Verify your FTP info (optional, but recommended)

Now we’ll make sure that our info is valid, and that we have full access to the website.  Open up FileZilla, type in your FTP info, and click “Quick connect”.  Wait for FileZilla to finish communicating with the server.  If it doesn’t work, then double-check the FTP information.  Once you have connected, close FZ and move on to the next step.


Finally, the actual application!

Now that we have retrieved and verified our FTP info, we can create an application that uploads an XML file to our webhost.  First, create a new Console Application with VB 2010.  Name it “AutomatedUploader”.

Don’t do anything in Sub Main for now.  Just create a new module called Util, and edit it to look like this:

Imports System.Net
Imports System.XML

Module Util

Function WriteXMLToFTP(ByVal FileName as String, Doc As XmlDocument) As Exception
    'Set up our FtpRequest. Replace yoursite.co.cc with your FTP host.
    Dim ftpRequest As System.Net.FtpWebRequest = _
        DirectCast(System.Net.WebRequest.Create("ftp://yoursite.co.cc/" + FileName), _
    'Create our credentials. Replace username with your FTP username, and password with your FTP password.
    ftpRequest.Credentials = New System.Net.NetworkCredential("username", "password")
    'Choose our request type. In this case, we want to upload a file.
    ftpRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile

    'Translate our file to binary.
    Dim xmlwrite As New XmlTextWriter("C:\TempXml.txt", System.Text.Encoding.ASCII)
    Dim bFile() As Byte = System.IO.File.ReadAllBytes("C:\TempXml.txt")

    'Now we can upload our file.
    Dim clsStream As System.IO.Stream = _
    'Write all data between the beginning and the end of our file to the stream.
    clsStream.Write(bFile, 0, bFile.Length)
    'Close the stream.
    'Dispose of our stream.
    'Return with no errors.
    Return New Exception("No errors")
Catch ex as Exception 'If there is an error...
    'Tell our caller.
    Return New Exception(ex.Message)
End Try
End Function

End Module

Now, in Sub Main, write this code, making sure that you import System.XML:

    'An XML writer to write an XML file with.
    Dim xmlwrite As New XmlTextWriter("C:\temp.xml", System.Text.Encoding.ASCII)
    'Write an arbitrary tag and value to the XML.
    xmlwrite.WriteElementString("AwesomeWebsite", "defenestrationcoding.co.cc")
    'Close the XML writer.
    'The document we will update.
    Dim xmldoc As New XmlDocument()
    'Load up our temporary XML file.
    'Delete 'temp.xml'. After all, that is what temp means.
    'Our filename.
    Dim name = "file.xml"
    'Upload our file to the 'net!
    WriteXMLToFTP(name, xmldoc)

Now, run your program. It should run without errors.

See your file on the ‘net!

Open FileZilla up again, and connect with your FTP info.  Now look closely in the right-hand explorer panel.  You should see the file: “file.xml”.  It will have this icon: .  Congratulations!  You just uploaded a file to the ‘net!  Don’t worry, no one can see it, but isn’t that cool?  Tomorrow, I’ll explain how to download files from the net. Auf Wiedersehen until then! UPDATE: Here’s the link to the next in the series –  https://defenestrationcoding.wordpress.com/2011/03/31/ftp-part-2/

Categories: Visual Basic Tags: , , ,
  1. onie
    February 10, 2013 at 5:25 AM

    how if i uplaod an image to may webhost?

  1. No trackbacks yet.

Your comment:

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: