Home > Visual Basic > Writing and Reading Files from the Internet via FTP (mIch cha')

Writing and Reading Files from the Internet via FTP (mIch cha')

Cool image of the internet

For those of you who are wondering, “mIch cha’ ” means, it is Klingon for Part (literally, sector) II.  Now, onto things that are actually related to the subject at hand. In yesterday’s post, I showed you how to upload files to a website in VB; today I’ll show you how to download a file.

Requirements: Yesterday’s finished project, your FTP info, today’s source (optional).

I’ll make it short and sweet today, seeing as yesterday’s post went a little long.

Step 1 of 2: Edit the source

First, add this function to Util:

'The function to read XML from a file.
Function ReadXMLFromFTP(ByVal FileName As String, ByVal 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 download a file.
            ftpRequest.Method = System.Net.WebRequestMethods.Ftp.DownloadFile

            'Get our buffer.
            Dim bFile(50) As Byte

            'Now we can download our file.
            Dim clsStream As System.IO.Stream = _
            'Write all data between the beginning and the end of our file to the stream.
            clsStream.Read(bFile, 0, bFile.Length)
            'Close the stream.
            'Dispose of our stream.

            'Convert the binary to XML.
            My.Computer.FileSystem.WriteAllBytes("c:\tmp.xml", bFile, False)

            '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

As you can see, ReadXmlFromFTP is pretty much the same as WriteXmlToFTP.  However, this time we are reading in the file, and our request type is DownloadFile.

Now, add the following to Sub Main:

        'Now, download the file back to our XML doc.
        ReadXMLFromFTP(name, xmldoc)
        'Display the contents of the XML file in the console.
        'Wait for the user to press Return.

This code reads the file back into xmldoc and outputs xmldoc. We also could have saved the file into memory, edited the file, gotten a value, etc.  Between today’s and yesterday’s tutorials, you should have a pretty good idea of how to manipulate data over the internet.

That’s all for today.  I hope that you have enjoyed reading this blog.  My parting question:  What have you learned from this site?


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: