Advanced Single-File Downloader
Posted: Sat Nov 14, 2009 3:41 am
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:
![Image]()
Now, add this code to Download click event:
Here is the source: Enjoy!
~GoodGuy17~
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
Add this below Public Class:
Imports System.Net
Code: Select all
Now, arrange your controls on the form similarly to this and change the captions of some controls to this: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, add this code to Download click event:
Code: Select all
Add this code to the Cancel click event:
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
Code: Select all
Add this code to the ... button click event:
Me.BackgroundWorker1.CancelAsync()
loc.Enabled = True
brws.Enabled = True
Code: Select all
Add this code to the backgroundworker dowork:
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
Code: Select all
Right click the form and click View Code and add this under all of the End Subs:
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)
Code: Select all
Test it out, and it should be working fine! This is my longest tutorial I've made wahooo; 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
Here is the source: Enjoy!
~GoodGuy17~
