Advanced Single-File Downloader

Heres your chance to share your own tutorials with the community. Just post them on here. If your lucky they may even be posted on the main site.
21 posts Page 1 of 3
GoodGuy17
Coding God
Coding God
Posts: 1610
Joined: Mon Sep 07, 2009 12:25 am

Advanced Single-File Downloader
GoodGuy17
Hello,
I am going to show you how to make your own file downloader.
First, let me explain what it does.
When you enter a real URL of a download, and choose where you want to save it, click download and it will show the stats of the download.
OK, start up VB.NET and add the following:
3 buttons - Names: brws, btn_download, btn_cancel
9 labels - Names: Label1, Label2, lblname, lbldownloading, lblsloc, lblsize, lblspd, lblstat, lblpercent
2 textboxes - Names: txtfilename, loc
1 progressbar - Name: ProgressBar1
1 FolderBrowserDialog - Name: FBD
1 SaveFileDialog - Name: SaveFileDialog1
1 BackgroundWorker - Name: BackgroundWorker1
You MUST put this above Public Class:
Code: Select all
Imports System.Net
Add this below Public Class:
Code: Select all
Dim whereToSave As String
    Delegate Sub ChangeTextsSafe(ByVal length As Long, ByVal position As Integer, ByVal percent As Integer, ByVal speed As Double)
    Delegate Sub DownloadCompleteSafe(ByVal cancelled As Boolean)
Now, arrange your controls on the form similarly to this and change the captions of some controls to this:
Image
Now, add this code to Download click event:
Code: Select all
        If Me.txtURL.Text <> "" AndAlso Me.txtfilename.Text.StartsWith("http://") Then
            Me.whereToSave = Me.loc.Text
            Me.SaveFileDialog1.FileName = ""
            Me.lblsloc.Text = "Save to : " & whereToSave
            Me.txtfilename.Enabled = False
            Me.btn_download.Enabled = False
            Me.btn_cancel.Enabled = True
            Me.loc.Enabled = False
            Me.brws.Enabled = False
            Me.BackgroundWorker1.RunWorkerAsync()
        Else
            MessageBox.Show("This url is not valid", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning)
        End If
Add this code to the Cancel click event:
Code: Select all
 Me.BackgroundWorker1.CancelAsync()
        loc.Enabled = True
        brws.Enabled = True
Add this code to the ... button click event:
Code: Select all
 Me.SaveFileDialog1.FileName = Me.txtfilename.Text.Split("/"c)(Me.txtfilename.Text.Split("/"c).Length - 1)
        Me.lblname.Text = "Name : " & Me.txtfilename.Text.Split("/"c)(Me.txtfilename.Text.Split("/"c).Length - 1)
        Me.SaveFileDialog1.ShowDialog()
        Me.loc.Text = Me.SaveFileDialog1.FileName
Add this code to the backgroundworker dowork:
Code: Select all
   Me.btn_download.Enabled = False
        Dim theResponse As HttpWebResponse
        Dim theRequest As HttpWebRequest
        Try
            theRequest = WebRequest.Create(Me.txtfilename.Text)
            theResponse = theRequest.GetResponse
        Catch ex As Exception
            MessageBox.Show("An error occurred while downloading file. Possibe causes:" & ControlChars.CrLf & _
                            "1) File doesn't exist" & ControlChars.CrLf & _
                            "2) Remote server error", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Dim cancelDelegate As New DownloadCompleteSafe(AddressOf DownloadComplete)
            Me.Invoke(cancelDelegate, True)
            Exit Sub
        End Try
        Dim length As Long = theResponse.ContentLength
        Dim safedelegate As New ChangeTextsSafe(AddressOf ChangeTexts)
        Me.Invoke(safedelegate, length, 0, 0, 0)
        Dim writeStream As New IO.FileStream(Me.whereToSave, IO.FileMode.Create)
        Dim nRead As Integer
        Dim speedtimer As New Stopwatch
        Dim currentspeed As Double = -1
        Dim readings As Integer = 0

        Do
            If BackgroundWorker1.CancellationPending Then
                Exit Do
            End If
            speedtimer.Start()
            Dim readBytes(4095) As Byte
            Dim bytesread As Integer = theResponse.GetResponseStream.Read(readBytes, 0, 4096)
            nRead += bytesread
            Dim percent As Short = (nRead / length) * 100
            Me.Invoke(safedelegate, length, nRead, percent, currentspeed)

            If bytesread = 0 Then Exit Do
            writeStream.Write(readBytes, 0, bytesread)
            speedtimer.Stop()
            readings += 1

            If readings >= 5 Then
                currentspeed = 20480 / (speedtimer.ElapsedMilliseconds / 1000)
                speedtimer.Reset()
                readings = 0
            End If
        Loop

        theResponse.GetResponseStream.Close()
        writeStream.Close()

        If Me.BackgroundWorker1.CancellationPending Then
            IO.File.Delete(Me.whereToSave)
            Dim cancelDelegate As New DownloadCompleteSafe(AddressOf DownloadComplete)
            Me.Invoke(cancelDelegate, True)
            Exit Sub
        End If

        Dim completeDelegate As New DownloadCompleteSafe(AddressOf DownloadComplete)
        Me.Invoke(completeDelegate, False)
Right click the form and click View Code and add this under all of the End Subs:
Code: Select all
Public Sub DownloadComplete(ByVal cancelled As Boolean)
        Me.txtFileName.Enabled = True
        Me.btn_download.Enabled = True

        If cancelled Then
            Me.btn_cancel.Enabled = False
            Me.lblstat.Text = "Status : " & "Cancelled"
            MessageBox.Show("Download Cancelled!", "Cancel", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Else
            loc.Enabled = True
            brws.Enabled = True
            Me.btn_cancel.Enabled = False
            Me.lblstat.Text = "Status : " & "Successfully downloaded"
            MessageBox.Show("Download Succeeded !", "Done", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If

        Me.ProgressBar1.Value = 0
    End Sub
    Public Sub ChangeTexts(ByVal length As Long, ByVal position As Integer, ByVal percent As Integer, ByVal speed As Double)

        Me.lblsize.Text = "Size : " & Math.Round((length / 1024), 2) & " KB"
        Me.lbldownloading.Text = "Downloading : " & Me.txtFileName.Text
        Me.lblstat.Text = "Status : " & Math.Round((position / 1024), 2) & " KB of " & Math.Round((length / 1024), 2) & "KB (" & Me.ProgressBar1.Value & "%)"
        Me.lblpercent.Text = Me.ProgressBar1.Value & "%"
        If speed = -1 Then
            Me.lblspd.Text = "Speed : " & "Calculating..."
        Else
            Me.lblspd.Text = "Speed : " & Math.Round((speed / 1024), 2) & " KB/s"
        End If

        Me.ProgressBar1.Value = percent
    End Sub
Test it out, and it should be working fine! This is my longest tutorial I've made wahooo;
Here is the source:
File Downloader.zip
Enjoy!
~GoodGuy17~ :D
You do not have the required permissions to view the files attached to this post.
Last edited by GoodGuy17 on Sat Nov 14, 2009 6:12 pm, edited 1 time in total.
User avatar
MasterCoding
Top Poster
Top Poster
Posts: 156
Joined: Sat Oct 24, 2009 4:26 pm

give a example please For my Operating System

ps. you get credits in the credits box
GoodGuy17
Coding God
Coding God
Posts: 1610
Joined: Mon Sep 07, 2009 12:25 am

Do you mean the source code?
User avatar
MasterCoding
Top Poster
Top Poster
Posts: 156
Joined: Sat Oct 24, 2009 4:26 pm

yes source code
User avatar
Nery
Co-Admin Moderator
Co-Admin Moderator
Posts: 1117
Joined: Mon Sep 07, 2009 8:11 pm

Re: Advanced Single-File Downloader
Nery
Pretty good, it'll be useful for me, since it is very advanced indeed.
GoodGuy17
Coding God
Coding God
Posts: 1610
Joined: Mon Sep 07, 2009 12:25 am

MasterCoding, I will post source code. Hold on.
GoodGuy17
Coding God
Coding God
Posts: 1610
Joined: Mon Sep 07, 2009 12:25 am

OK, I posted the source and added a cancel button to the tutorial! :D
User avatar
RunarM
Hardcore Programmer
Hardcore Programmer
Posts: 508
Joined: Wed Nov 18, 2009 11:33 pm

Re: Advanced Single-File Downloader
RunarM
Could anyone help me make it like this:

You have a combo box [txtfilename] and like 10+ downloading options, and when you click on one of them then download then it downloades em.

Really need help o.O
Just another day in my life.
http://www.codexvideos.com
User avatar
lawrence12
Dedicated Member
Dedicated Member
Posts: 73
Joined: Tue Mar 02, 2010 3:21 am

whats the txtURL?
am i going to add three textboxes? or just 2
i named the first textbox
txtfilename
and the second textbox
loc
and then an error appeared it says
txtURL is not a member of My Form
:? can you please help me?
User avatar
jeezy09
VIP - Donator
VIP - Donator
Posts: 25
Joined: Wed Mar 31, 2010 1:51 am

Re: Advanced Single-File Downloader
jeezy09
i saw this exact same thing on youtube lol cool program though
21 posts Page 1 of 3
Return to “Tutorials”