Results 1 to 3 of 3

Thread: [RESOLVED] [2008] Possible to add progress to this code?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2008
    Location
    Nowhere
    Posts
    427

    Resolved [RESOLVED] [2008] Possible to add progress to this code?

    I want to add progress to this code that uses Net.WebRequest to download a list of files:

    vb.net Code:
    1. Public Class Form1
    2.     Private Const filenames As String = "c:\Users\Home\Desktop\list_of_files.txt"
    3.     Private Const url As String = "http://www.fileden.com/files/2008/12/22/2233952"
    4.     Private Const savepath As String = "c:\Users\Home\Desktop\"
    5.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    6.         Dim sr As New IO.StreamReader(filenames)
    7.         Dim line As String = sr.ReadLine()
    8.         Dim req As Net.WebRequest
    9.         Dim resp As IO.Stream
    10.         Dim out As IO.BinaryWriter
    11.         Do While line IsNot Nothing
    12.             req = Net.HttpWebRequest.Create(url & line)
    13.             resp = req.GetResponse().GetResponseStream()
    14.             out = New IO.BinaryWriter(New IO.FileStream(savepath & line, IO.FileMode.OpenOrCreate))
    15.             Dim buf(4096) As Byte
    16.             Dim k As Int32 = resp.Read(buf, 0, 4096)
    17.             Do While k > 0
    18.                 out.Write(buf, 0, k)
    19.                 k = resp.Read(buf, 0, 4096)
    20.             Loop
    21.             resp.Close()
    22.             out.Close()
    23.             line = sr.ReadLine()
    24.         Loop
    25.     End Sub
    26.  
    27.  
    28. End Class

    Is this possible? If it is how would i do it? Also is the connection secure because i plan to use credentials to log onto the server?
    "Programming is like sex. One mistake and you have to support it for the rest of your life." ~Michael Sinz


    Code Snippets/Usefull Links:
    WinRAR DLL|Vista Style Form|Krypton Component Library|Windows Form Aero|Parsing XML files|Calculate File's CRC 32|Download a list of files.

  2. #2
    PowerPoster Deepak Sakpal's Avatar
    Join Date
    Mar 2002
    Location
    Mumbai, India
    Posts
    2,424

    Re: [2008] Possible to add progress to this code?

    Yes, it is possible. First you need to get the size of the file that you want to download and set it to the Maximum value of progress bar. Next, when you are downloading files in chunks your set that value of bytes read to the value of progress bar. Here is an example posted in CodeBank by Kleinma and here is an another example.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2008
    Location
    Nowhere
    Posts
    427

    Re: [2008] Possible to add progress to this code?

    Thanks lot i got it to report progress but how would i go about getting the download speed and getting the amount that has been correctly downloaded? This is where my code is ATM, it downloads all files from the list and returns progress in the progbar:
    vb.net Code:
    1. Imports System.IO
    2. Imports System.Net
    3. Public Class Form1
    4.     Private Const filenames As String = "c:\Users\Home\Desktop\list_of_files.txt"
    5.     Private Const url As String = "http://www.fileden.com/files/2008/12/22/2233952"
    6.     Private Const savepath As String = "c:\Users\Home\Desktop"
    7.  
    8.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    9.         Try
    10.             Dim sr As New IO.StreamReader(filenames)
    11.             Dim line As String = sr.ReadLine()
    12.             Dim req As Net.WebRequest
    13.             Dim resp As IO.Stream
    14.             Dim out As IO.BinaryWriter
    15.             Dim URLRes As Net.HttpWebResponse
    16.             Dim iBytesRead As Integer
    17.             Do While line IsNot Nothing
    18.                 req = Net.HttpWebRequest.Create(url & line)
    19.                 resp = req.GetResponse().GetResponseStream()
    20.                 URLRes = req.GetResponse
    21.                 pprogress.Maximum = URLRes.ContentLength
    22.                 out = New IO.BinaryWriter(New IO.FileStream(savepath & line, IO.FileMode.OpenOrCreate))
    23.                 Dim buf(4096) As Byte
    24.                 Dim k As Int32 = resp.Read(buf, 0, 4096)
    25.                 iBytesRead = resp.Read(buf, 0, 4096)
    26.                 Do While k > 0
    27.                     out.Write(buf, 0, k)
    28.                     k = resp.Read(buf, 0, 4096)
    29.                     If pprogress.Value + iBytesRead <= pprogress.Maximum Then
    30.                         pprogress.Value += iBytesRead
    31.                     Else
    32.                         pprogress.Value = pprogress.Maximum
    33.                     End If
    34.                 Loop
    35.                 resp.Close()
    36.                 out.Close()
    37.                 line = sr.ReadLine()
    38.             Loop
    39.         Catch dlerror As Exception
    40.             MsgBox(dlerror.Message)
    41.         End Try
    42.     End Sub
    43. End Class
    Last edited by youngbucks; Dec 23rd, 2008 at 11:09 AM.
    "Programming is like sex. One mistake and you have to support it for the rest of your life." ~Michael Sinz


    Code Snippets/Usefull Links:
    WinRAR DLL|Vista Style Form|Krypton Component Library|Windows Form Aero|Parsing XML files|Calculate File's CRC 32|Download a list of files.

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