Results 1 to 9 of 9

Thread: How do you download a file?

Hybrid View

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2006
    Posts
    2

    How do you download a file?

    Right I apologize if this has been asked before, but I need to find an answer as I start my major college project in a few weeks.

    Right, I am planning on making a program that will let the user search through a database then download files that are related to their search. I need to be able to do two things.

    1) Acces a file and download it. Whilst it downloads I need it to report the progress to the progress bar.
    2) Depending on where I keep the database (online or locally) I will need the program to automaticaly download new database updates. This one is optional because like I said it depends on if I put the database online or bundle it with the program.

    I appreciate any help or links to tutorials

    Many thanks in advance, Jake.

  2. #2
    Fanatic Member Mr.No's Avatar
    Join Date
    Sep 2002
    Location
    Mauritius
    Posts
    651

    Re: How do you download a file?

    To begin with, you need to read about HttpWebRequest and FtpWebRequest (in .NET 2.0 only).
    Using VB.NET 2003/.NET 1.1/C# 2.0
    http://del.icio.us/rajoo
    Blow your mind, smoke gunpowder
    Ashes to ashes, dust to dust
    If God won't have you, the devil will. - Author unknown
    Don't follow me, I'm lost too ...

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2006
    Posts
    2

    Re: How do you download a file?

    OK, I have found methods of downloading using those, I got the HTTpWebRequest one working, but what I am struggling with now is how to implement a progress ba with that.

    Any suggestions?

  4. #4
    Fanatic Member Mr.No's Avatar
    Join Date
    Sep 2002
    Location
    Mauritius
    Posts
    651

    Re: How do you download a file?

    Try this, I think I found it at in an article at http://www.c-sharpcorner.com but not sure about the exact link (Thanks to the original author).

    VB Code:
    1. Private Sub DownloadFile(byval filetoDownload)
    2.     Dim req As HttpWebRequest
    3.     Dim wc As WebClient
    4.     Dim client As WebClient
    5.     Dim data As Stream
    6.     Dim reader As StreamReader
    7.     Dim wProxy As WebProxy
    8.     Dim str As String = ""
    9.    
    10.     Try
    11.       client = New WebClient
    12.  
    13.  
    14.       Dim URL As String = filetoDownload  ' e.g http://www.abc.com/file.txt"
    15.  
    16.      
    17.       data = client.OpenRead(URL)
    18.            
    19.  
    20.       reader = New StreamReader(data)
    21.      
    22.       dim allData as String
    23.       allData = reader.ReadToEnd())
    24.       ' add  necessary code to write this string to a file
    25.  
    26.     Catch ex As Exception
    27.       MessageBox.Show(ex.ToString())
    28.     End Try
    29.   End Sub
    Using VB.NET 2003/.NET 1.1/C# 2.0
    http://del.icio.us/rajoo
    Blow your mind, smoke gunpowder
    Ashes to ashes, dust to dust
    If God won't have you, the devil will. - Author unknown
    Don't follow me, I'm lost too ...

  5. #5
    Addicted Member
    Join Date
    Jun 2005
    Posts
    243

    Re: How do you download a file?

    This is what I use.

    WebAddress = "http://www.abc.com/file.txt"


    VB Code:
    1. Private Function DownloadFile(ByVal WebAddress As String) As String
    2.         Dim webfile As New WebClient
    3.         Dim URL As String = String.Empty
    4.  
    5.         Try
    6.             strDoc = WebAddress.Substring(WebAddress.LastIndexOf("/"c) + 1)
    7.             If File.Exists(strDoc) = False Then
    8.                 URL = WebAddress.Trim
    9.                 webfile.DownloadFile(URL, strDoc)
    10.             End If
    11.             DownloadFile = ""
    12.         Catch ex As Exception
    13.             DownloadFile = ex.Message
    14.         Finally
    15.             webfile.Dispose()
    16.         End Try
    17.     End Function

  6. #6
    Fanatic Member Mr.No's Avatar
    Join Date
    Sep 2002
    Location
    Mauritius
    Posts
    651

    Re: How do you download a file?

    Yep, Zombie_man23's solution is the right one if you're using VB.NET 2005. Mine was written for VB.NET 2003.
    Using VB.NET 2003/.NET 1.1/C# 2.0
    http://del.icio.us/rajoo
    Blow your mind, smoke gunpowder
    Ashes to ashes, dust to dust
    If God won't have you, the devil will. - Author unknown
    Don't follow me, I'm lost too ...

  7. #7
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: How do you download a file?

    If you are using 2005 I think you can use this putting in the information as you need it:

    vb Code:
    1. My.Computer.Network.DownloadFile("address", "destination", "userName Here", "Password Here")

    Although there must be a way of doing it with the WebClient as well. Have you checked out the webClient documentation on MSDN to find the answer??
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  8. #8
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: How do you download a file?

    look at my signature for example code on download a file with progress bar

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width