Jump to content





Recent Status Updates

  • Photo
      16 Apr
    Kadence

    If you're reading this, you're on my profile and I know you're on my profile because I'm probably viewing yours.

    Show comments (6)
  • Photo
      10 Apr
    Poe

    Finally (and hopefully) i'm getting a team together that knows a little of this and a little of that; and maybe all my open source projects that are half written can begin to be released. :)

View All Updates
Photo
- - - - -

Download and Upload using VB.NET

VB.NET

  • Please log in to reply
8 replies to this topic

#1 Vswe

Vswe

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1,989 posts
  • Programming Language:Java, C#, PHP, Python, JavaScript, PL/SQL, Visual Basic .NET, Lua, ActionScript

Posted 09 November 2009 - 03:34 PM

I will in this tutorial show you how you can download and upload files using VB.NET. The download is easier then the upload. Since both Downloading and Uploading can take some time I'm recommending using Multi threading, I will not cover how to do that in this tutorial so therefor I won't use it in the tutorial, if you want to learn how to use multi threading you can read this tutorial. http://forum.codecal...-basic-net.html Both Downloading and Uploading is pretty easy to use since they only use one line of code each, but even since that is the case there's pretty much you have to learn about them, which will be covered in this tutorial.





Downloading

To download a file to your computer you'll use:

My.Computer.Network.DownloadFile()


The most simple way for downloading a file is to only set a string for the URL to download from and a string for the path telling us where on the computer it should be saved on. But since we'll later on(with more advanced downloading options) can't use a string for the URL since that will not be a combination of parameters that exists we'll start to use the Uri type from the beginning, to create a new Uri you just type new Uri([the URL as string]). So it will basically be just like adding it as a string. So to download a file you can do something like this:

 My.Computer.Network.DownloadFile(New Uri("http://vswe.codecall.net/Test.txt"), "C:/Test.txt")



Most files that could be downloaded on the internet doesn't need any username or password to be downloaded, so username and password will only be used when downloading private files on your own server, when uploading files you'll have to add usernames and password most of the time since you can't upload files to just any web site, you need to have access to upload them, but more to that later. Since we only want to download "normal" files we'll just leave username and password blank:


My.Computer.Network.DownloadFile(New Uri("http://vswe.codecall.net/Test.txt"), "C:/Test.txt", "", "")



Now we we can also add some UI fi we want, this UI is a box showing the file which is being downloaded, the progress for that file and also a cancel button, when pressing this cancel button the default thing is that an exception is thrown but we can change that later if we want to. The default value for the UI is false so if you don't want to add it just write False instead, but we're going to add the, but there's no option only adding the UI. To be able to set it we also have to set connectionTimeout and overwrite.


ConnectionTimeout is how long time in milliseconds it will try to download the file before it gives up, the default value is 100 seconds (100,000 milliseconds).

Overwrites is a boolean value indicating if it will try to overwrite a file if it already exists, this value is false by default and if it's false and the file does already exists an error will occur. By adding these three values our code can now look like this:


        My.Computer.Network.DownloadFile(New Uri("http://vswe.codecall.net/Test.txt"), "C:/Test.txt", "", "", True, 60000, True)



Now we can also set what will happen if the user clicked cancel on the UI. The default value is FileIO.UICancelOption.ThrowException which will throw an exception as you can hear, the other thing is that you set it to don't do anything if the user clicks cancel by using FileIO.UICancelOption.DoNothing. Now our downloading could look like this:


        My.Computer.Network.DownloadFile(New Uri("http://vswe.codecall.net/Test.txt"), "C:/Test.txt", "", "", True, 60000, True, FileIO.UICancelOption.DoNothing)









Uploading


To upload a file to a server you use:

My.Computer.Network.UploadFile()



I won't explain the Uploading as much as the downloading since many things works the same in the both of them. When uploading you're writing the local path on the computer first, you'll always write where you're getting the file from first and then where you want to upload/download it to. When uploading a file you need to have a server to upload it to, you also need a username and a password to the server to be able to upload the file to it. Also note how the example URL looks like:


My.Computer.Network.UploadFile("C:\Test.txt", New Uri("ftp://vswe.codecall.net/public_ftp/Test.txt"), "******", "*******")





And the rest of the uploading features is the same as for downloading so I won't teach you anything more about uploading. So that was this tutorial. I hope you enjoyed it and also learned something from it :)
  • 2

#2 Guest_Jordan_*

Guest_Jordan_*
  • Guest

Posted 10 November 2009 - 05:21 AM

Cool! I never knew about these functions. Could be very handy for downloading an update to your own program. :)
+rep
  • 0

#3 Vswe

Vswe

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1,989 posts
  • Programming Language:Java, C#, PHP, Python, JavaScript, PL/SQL, Visual Basic .NET, Lua, ActionScript

Posted 10 November 2009 - 05:31 AM

There's special functions for that :)
  • 0

#4 Guest_Jordan_*

Guest_Jordan_*
  • Guest

Posted 10 November 2009 - 05:33 AM

Do I feel another tutorial coming?
  • 0

#5 Vswe

Vswe

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1,989 posts
  • Programming Language:Java, C#, PHP, Python, JavaScript, PL/SQL, Visual Basic .NET, Lua, ActionScript

Posted 10 November 2009 - 05:35 AM

Yes of course. But I'm not sure if it'll be about that, I'm not so familiar with it, I only know it exists. But don't count that out :)
  • 0

#6 WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderator
  • 16,635 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 13 November 2009 - 08:13 AM

Very useful to know. +rep
  • 0
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#7 SimonCoder

SimonCoder

    CC Lurker

  • Just Joined
  • Pip
  • 6 posts

Posted 09 June 2010 - 07:01 AM

Question? Can files be downloaded from a network in this same way rather than from a website?

for example:
My.Computer.Network.DownloadFile(New Uri("//SERVER/files/myfolder/otherfolder/Test.txt"), "C:/Test.txt", "", "")

  • 0

#8 bbqroast

bbqroast

    Codecall Addict

  • Senior Member
  • PipPipPipPipPipPip
  • 564 posts
  • Location:/etc/passwd

Posted 03 September 2010 - 08:38 PM

Could i do some thing like this:
Download file www.epicguy.com/update.txt
Update .txt contains a version number thats all
If the number is higher than the version the program is then:
Download file www.epicguy.com/updateaddress.txt
In their it contains the file name of the most recent release, VB stores it as a variable FILE
Download file www.epicguy.com/files/FILE
Or is their and easier way (like storing both the file name and the version NO in the same text file??)
  • 0
Please, write clearly with proper structure. Double spacing makes the text feel un-jointed, Capitalizing Every Word Means People Stop Before Every Word Sub-Consciously Which Is A Pain In The Backside, and use code tags! (The right most styling box).

#9 SimonCoder

SimonCoder

    CC Lurker

  • Just Joined
  • Pip
  • 6 posts

Posted 03 September 2010 - 08:53 PM

Could i do some thing like this:

Download file www.epicguy.com/update.txt
Update .txt contains a version number thats all
If the number is higher than the version the program is then:
Download file www.epicguy.com/updateaddress.txt
In their it contains the file name of the most recent release, VB stores it as a variable FILE
Download file www.epicguy.com/files/FILE
Or is their and easier way (like storing both the file name and the version NO in the same text file??)


I need to know a little more about what your asking... do you want to download the two text files and combine them then write them to one text file?
  • 0