Click here to Skip to main content
15,895,530 members
Articles / Programming Languages / Visual Basic
Article

Zezo WebFiles Downloader

Rate me:
Please Sign up or sign in to vote.
3.60/5 (6 votes)
20 Sep 2008CPOL 28.5K   1K   21   5
This is a program which can download any file from the Internet
Image 1

2.png

Introduction

This is a program which can download any file from the Internet.

Background

I used a class to control the download and renamed it to WebFileDownloader.

The WebFileDownloader class provides methods for downloading a file from a URL and firing events to update progress in the GUI on a progress bar or whatever you may like.

The progress is returned as a long, and a progress bar takes an integer. So, I convert the long to integer.

Note

You should put a direct full URL like http://www.****.com/filename.rar.

Put this code on the main form:

VB.NET
Public Class form1
Inherits System.Windows.Forms.Form
Private Const URL_MESSAGE As String = "Enter URL of file here"
Private Const DIR_MESSAGE As String = "Enter directory to download to here"
Private WithEvents _Downloader As WebFileDownloader
Dim x As Boolean
#Region " Windows Form Designer generated code "
 ublic Sub New()
MyBase.New()
InitializeComponent()
End Sub
#End Region
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
Public Shared Sub Main()
Application.EnableVisualStyles()
Application.DoEvents()
Application.Run(New form1)
Application.Exit()
End Sub
Private Sub textbox1_GotFocus(ByVal sender As System.Object, _
	ByVal e As System.EventArgs) Handles TextBox1.GotFocus
If TextBox1.Text = URL_MESSAGE Then TextBox1.Text = String.Empty
End Sub
Private Sub txtURL_LostFocus(ByVal sender As System.Object, _
	ByVal e As System.EventArgs) Handles TextBox1.LostFocus
If TextBox1.Text = String.Empty Then TextBox1.Text = URL_MESSAGE
End Sub
Private Sub textbox2_GotFocus(ByVal sender As System.Object, _
	ByVal e As System.EventArgs) Handles TextBox2.GotFocus
If TextBox2.Text = DIR_MESSAGE Then TextBox2.Text = String.Empty
End Sub
Private Sub textbox2_LostFocus(ByVal sender As System.Object, _
	ByVal e As System.EventArgs) Handles TextBox2.LostFocus
If TextBox2.Text = String.Empty Then TextBox2.Text = DIR_MESSAGE
End Sub
Private Sub button1_Click(ByVal sender As System.Object, _
	ByVal e As System.EventArgs) Handles Button1.Click
x = True
End
End Sub
Private Sub button2_Click(ByVal sender As System.Object, _
	ByVal e As System.EventArgs) Handles Button2.Click
If Not IO.Directory.Exists(TextBox2.Text) Then
MessageBox.Show("Not a valid directory to download to, _
	please pick a valid directory", "Error", _
	MessageBoxButtons.OK, MessageBoxIcon.Error)
Return
End If
Try
_Downloader = New WebFileDownloader
_Downloader.DownloadFileWithProgress(TextBox1.Text, _
	TextBox2.Text.TrimEnd("\"c) & GetFileNameFromURL(TextBox1.Text))
Catch ex As Exception
MessageBox.Show("Error: " & ex.Message)
End Try
End Sub
Private Function GetFileNameFromURL(ByVal URL As String) As String
If URL.IndexOf("/"c) = -1 Then Return String.Empty
Return "\" & URL.Substring(URL.LastIndexOf("/"c) + 1)
End Function
Private Sub button3_Click(ByVal sender As System.Object, _
	ByVal e As System.EventArgs) Handles Button3.Click
If FolderBrowserDialog1.ShowDialog(Me) <> DialogResult.Cancel Then
TextBox2.Text = FolderBrowserDialog1.SelectedPath
End If
End Sub
Private Sub _Downloader_FileDownloadSizeObtained(ByVal iFileSize As Long) _
	Handles _Downloader.FileDownloadSizeObtained
ProgressBar1.Value = 0
ProgressBar1.Maximum = Convert.ToInt32(iFileSize)
End Sub
Private Sub _Downloader_FileDownloadComplete() Handles _Downloader.FileDownloadComplete
ProgressBar1.Value = ProgressBar1.Maximum
MessageBox.Show("File Download Complete")
End Sub
Private Sub _Downloader_FileDownloadFailed(ByVal ex As System.Exception) _
	Handles _Downloader.FileDownloadFailed
MessageBox.Show("An error has occurred during download: " & ex.Message)
End Sub
Private Sub _Downloader_AmountDownloadedChanged(ByVal iNewProgress As Long) _
	Handles _Downloader.AmountDownloadedChanged
ProgressBar1.Value = Convert.ToInt32(iNewProgress)
lblProgress.Text = WebFileDownloader.FormatFileSize(iNewProgress) & _
    " of " & WebFileDownloader.FormatFileSize(ProgressBar1.Maximum) & " downloaded"
Application.DoEvents()
End Sub
Private Sub form1_Load(ByVal sender As System.Object, _
	ByVal e As System.EventArgs) Handles MyBase.Load
lblProgress.Text = String.Empty
TextBox1.Text = URL_MESSAGE
TextBox2.Text = DIR_MESSAGE
Show()
Button3.Focus()
End Sub
Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, _
	ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) _
	Handles LinkLabel1.LinkClicked
System.Diagnostics.Process.Start("mailto:eng_mah050@yahoo.com")
End Sub
End Class

Put this in the class:

VB.NET
Imports System.Net
Imports System.IO
Public Class WebFileDownloader
Public Event AmountDownloadedChanged(ByVal iNewProgress As Long)
Public Event FileDownloadSizeObtained(ByVal iFileSize As Long)
Public Event FileDownloadComplete()
Public Event FileDownloadFailed(ByVal ex As Exception)
Private mCurrentFile As String = String.Empty
Public ReadOnly Property CurrentFile() As String
Get
Return mCurrentFile
End Get
End Property
Public Function DownloadFile(ByVal URL As String, ByVal Location As String) As Boolean
Try
mCurrentFile = GetFileName(URL)
Dim WC As New WebClient
WC.DownloadFile(URL, Location)
RaiseEvent FileDownloadComplete()
Return True
Catch ex As Exception
RaiseEvent FileDownloadFailed(ex)
Return False
End Try
End Function
Private Function GetFileName(ByVal URL As String) As String
Try
Return URL.Substring(URL.LastIndexOf("/") + 1)
Catch ex As Exception
Return URL
End Try
End Function
Public Function DownloadFileWithProgress_
	(ByVal URL As String, ByVal Location As String) As Boolean
Dim FS As FileStream
Try
mCurrentFile = GetFileName(URL)
Dim wRemote As WebRequest
Dim bBuffer As Byte()
ReDim bBuffer(256)
Dim iBytesRead As Integer
Dim iTotalBytesRead As Integer
FS = New FileStream(Location, FileMode.Create, FileAccess.Write)
wRemote = WebRequest.Create(URL)
Dim myWebResponse As WebResponse = wRemote.GetResponse
RaiseEvent FileDownloadSizeObtained(myWebResponse.ContentLength)
Dim sChunks As Stream = myWebResponse.GetResponseStream
Do
iBytesRead = sChunks.Read(bBuffer, 0, 256)
FS.Write(bBuffer, 0, iBytesRead)
iTotalBytesRead += iBytesRead
If myWebResponse.ContentLength < iTotalBytesRead Then
RaiseEvent AmountDownloadedChanged(myWebResponse.ContentLength)
Else
RaiseEvent AmountDownloadedChanged(iTotalBytesRead)
End If
Loop While Not iBytesRead = 0
sChunks.Close()
FS.Close()
RaiseEvent FileDownloadComplete()
Return True
Catch ex As Exception
If Not (FS Is Nothing) Then
FS.Close()
FS = Nothing
End If
RaiseEvent FileDownloadFailed(ex)
Return False
End Try
End Function
Public Shared Function FormatFileSize(ByVal Size As Long) As String
Try
Dim KB As Integer = 1024
Dim MB As Integer = KB * KB
If Size < KB Then
Return (Size.ToString("D") & " bytes")
Else
Select Case Size / KB
Case Is < 1000
Return (Size / KB).ToString("N") & "KB"
Case Is < 1000000
Return (Size / MB).ToString("N") & "MB"
Case Is < 10000000
Return (Size / MB / KB).ToString("N") & "GB"
End Select
End If
Catch ex As Exception
Return Size.ToString
End Try
End Function
End Class       

I await your feedback.

History

  • 20th September, 2008: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Engineer
Egypt Egypt
My name is Mahmoud abdel aziz elgindy
I'm from EGYPT
I'm 31 years old.
Senior Penetration tester.
My interests :
***Programming with (VB.NET ,Python ,Assembly ,Linux Scripting ).
***Reading books.

Comments and Discussions

 
QuestionAmazing Pin
Alec Wham9-Mar-17 15:00
Alec Wham9-Mar-17 15:00 
GeneralMy vote of 5 Pin
Mahmoud Elgindy23-Sep-13 5:59
Mahmoud Elgindy23-Sep-13 5:59 
GeneralIt looks nice Pin
Shane Story7-Oct-09 10:45
Shane Story7-Oct-09 10:45 
GeneralYa Mahmoud Pin
Jamal Alqabandi22-Sep-08 23:54
Jamal Alqabandi22-Sep-08 23:54 
GeneralRe: Ya Mahmoud Pin
Mahmoud Elgindy7-Dec-08 0:01
Mahmoud Elgindy7-Dec-08 0:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.