Downloading with progress bar

i got the code, but how do i make i work with the rest?

Page 1 of 1

0 Replies - 1947 Views - Last Post: 15 March 2009 - 05:40 PM Rate Topic: -----

#1 EndLessMind  Icon User is offline

  • These are the droids you're looking for
  • member icon

Reputation: 194
  • View blog
  • Posts: 1,099
  • Joined: 13-March 09

Downloading with progress bar

Post icon  Posted 15 March 2009 - 05:40 PM

okey.. now im really stuck. i got a code for downloading woth progress bar.. and guess what.. i dont know how to "connect it" to the rest of my code.. de new code is
'2006 kleinma MSMVP
'www.vbforums.com

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
			' Return size of file in kilobytes.
			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

	Protected Overrides Sub Finalize()
		MyBase.Finalize()
	End Sub
End Class




and i also got this one.. but i dont know if i need it
 
Public Class frmMain
	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"

	'DECLARE THIS WITHEVENTS SO WE GET EVENTS ABOUT DOWNLOAD PROGRESS
	Private WithEvents _Downloader As WebFileDownloader

#Region " Windows Form Designer generated code "

	Public Sub New()
		MyBase.New()

		'This call is required by the Windows Form Designer.
		InitializeComponent()

		'Add any initialization after the InitializeComponent() call

	End Sub

	'Form overrides dispose to clean up the component list.
	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

	'Required by the Windows Form Designer
	Private components As System.ComponentModel.IContainer

	'NOTE: The following procedure is required by the Windows Form Designer
	'It can be modified using the Windows Form Designer.  
	'Do not modify it using the code editor.
	Friend WithEvents txtURL As System.Windows.Forms.TextBox
	Friend WithEvents ProgBar As System.Windows.Forms.ProgressBar
	Friend WithEvents cmdDownload As System.Windows.Forms.Button
	Friend WithEvents cmdClose As System.Windows.Forms.Button
	Friend WithEvents cmdGetFolder As System.Windows.Forms.Button
	Friend WithEvents txtDownloadTo As System.Windows.Forms.TextBox
	Friend WithEvents dlgFolderBrowse As System.Windows.Forms.FolderBrowserDialog
	Friend WithEvents lblProgress As System.Windows.Forms.Label
	Friend WithEvents lnkForums As System.Windows.Forms.LinkLabel
	Friend WithEvents lnkMe As System.Windows.Forms.LinkLabel
	<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
		Me.txtURL = New System.Windows.Forms.TextBox
		Me.ProgBar = New System.Windows.Forms.ProgressBar
		Me.cmdDownload = New System.Windows.Forms.Button
		Me.cmdClose = New System.Windows.Forms.Button
		Me.cmdGetFolder = New System.Windows.Forms.Button
		Me.txtDownloadTo = New System.Windows.Forms.TextBox
		Me.dlgFolderBrowse = New System.Windows.Forms.FolderBrowserDialog
		Me.lblProgress = New System.Windows.Forms.Label
		Me.lnkForums = New System.Windows.Forms.LinkLabel
		Me.lnkMe = New System.Windows.Forms.LinkLabel
		Me.SuspendLayout()
		'
		'txtURL
		'
		Me.txtURL.Location = New System.Drawing.Point(8, 8)
		Me.txtURL.Name = "txtURL"
		Me.txtURL.Size = New System.Drawing.Size(376, 20)
		Me.txtURL.TabIndex = 0
		Me.txtURL.Text = "Type URL Here"
		'
		'ProgBar
		'
		Me.ProgBar.Location = New System.Drawing.Point(8, 72)
		Me.ProgBar.Name = "ProgBar"
		Me.ProgBar.Size = New System.Drawing.Size(376, 16)
		Me.ProgBar.TabIndex = 1
		'
		'cmdDownload
		'
		Me.cmdDownload.FlatStyle = System.Windows.Forms.FlatStyle.System
		Me.cmdDownload.Location = New System.Drawing.Point(312, 96)
		Me.cmdDownload.Name = "cmdDownload"
		Me.cmdDownload.Size = New System.Drawing.Size(72, 24)
		Me.cmdDownload.TabIndex = 3
		Me.cmdDownload.Text = "Download"
		'
		'cmdClose
		'
		Me.cmdClose.FlatStyle = System.Windows.Forms.FlatStyle.System
		Me.cmdClose.Location = New System.Drawing.Point(312, 128)
		Me.cmdClose.Name = "cmdClose"
		Me.cmdClose.Size = New System.Drawing.Size(72, 24)
		Me.cmdClose.TabIndex = 4
		Me.cmdClose.Text = "Close"
		'
		'cmdGetFolder
		'
		Me.cmdGetFolder.FlatStyle = System.Windows.Forms.FlatStyle.System
		Me.cmdGetFolder.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
		Me.cmdGetFolder.Location = New System.Drawing.Point(352, 40)
		Me.cmdGetFolder.Name = "cmdGetFolder"
		Me.cmdGetFolder.Size = New System.Drawing.Size(32, 20)
		Me.cmdGetFolder.TabIndex = 2
		Me.cmdGetFolder.Text = "..."
		Me.cmdGetFolder.TextAlign = System.Drawing.ContentAlignment.BottomCenter
		'
		'txtDownloadTo
		'
		Me.txtDownloadTo.Location = New System.Drawing.Point(8, 40)
		Me.txtDownloadTo.Name = "txtDownloadTo"
		Me.txtDownloadTo.Size = New System.Drawing.Size(344, 20)
		Me.txtDownloadTo.TabIndex = 1
		Me.txtDownloadTo.Text = ""
		'
		'lblProgress
		'
		Me.lblProgress.Location = New System.Drawing.Point(8, 96)
		Me.lblProgress.Name = "lblProgress"
		Me.lblProgress.Size = New System.Drawing.Size(272, 16)
		Me.lblProgress.TabIndex = 8
		Me.lblProgress.Text = "#Progress"
		'
		'lnkForums
		'
		Me.lnkForums.Location = New System.Drawing.Point(8, 136)
		Me.lnkForums.Name = "lnkForums"
		Me.lnkForums.Size = New System.Drawing.Size(128, 16)
		Me.lnkForums.TabIndex = 10
		Me.lnkForums.TabStop = True
		Me.lnkForums.Text = "www.vbforums.com"
		'
		'lnkMe
		'
		Me.lnkMe.Location = New System.Drawing.Point(8, 120)
		Me.lnkMe.Name = "lnkMe"
		Me.lnkMe.Size = New System.Drawing.Size(184, 16)
		Me.lnkMe.TabIndex = 11
		Me.lnkMe.TabStop = True
		Me.lnkMe.Text = "Code by kleinma - MSMVP"
		'
		'frmMain
		'
		Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
		Me.ClientSize = New System.Drawing.Size(392, 160)
		Me.Controls.Add(Me.lnkMe)
		Me.Controls.Add(Me.lnkForums)
		Me.Controls.Add(Me.lblProgress)
		Me.Controls.Add(Me.cmdGetFolder)
		Me.Controls.Add(Me.txtDownloadTo)
		Me.Controls.Add(Me.cmdClose)
		Me.Controls.Add(Me.cmdDownload)
		Me.Controls.Add(Me.ProgBar)
		Me.Controls.Add(Me.txtURL)
		Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
		Me.MaximizeBox = False
		Me.Name = "frmMain"
		Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
		Me.Text = "Download File With Progress"
		Me.ResumeLayout(False)

	End Sub

#End Region

	'SUB MAIN WHERE WE ENABLE VISUAL STYLES, AND RUN MAIN FORM
	Public Shared Sub Main()
		Application.EnableVisualStyles()
		Application.DoEvents()
		Application.Run(New frmMain)
		Application.Exit()
	End Sub

	'THESE ARE JUST FOR GUI TO MAKE IT LOOK A LITTLE MORE NICE
	Private Sub txtURL_GotFocus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtURL.GotFocus
		If txtURL.Text = URL_MESSAGE Then txtURL.Text = String.Empty
	End Sub
	Private Sub txtURL_LostFocus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtURL.LostFocus
		If txtURL.Text = String.Empty Then txtURL.Text = URL_MESSAGE
	End Sub
	Private Sub txtDownloadTo_GotFocus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDownloadTo.GotFocus
		If txtDownloadTo.Text = DIR_MESSAGE Then txtDownloadTo.Text = String.Empty
	End Sub
	Private Sub txtDownloadTo_LostFocus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDownloadTo.LostFocus
		If txtDownloadTo.Text = String.Empty Then txtDownloadTo.Text = DIR_MESSAGE
	End Sub

	'CLOSE PROGRAM
	Private Sub cmdClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClose.Click
		Me.Close()
	End Sub

	Private Sub cmdDownload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDownload.Click

		'VERIFY A DIRECTORY WAS PICKED AND THAT IT EXISTS
		If Not IO.Directory.Exists(txtDownloadTo.Text) Then
			MessageBox.Show("Not a valid directory to download to, please pick a valid directory", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Return
		End If

		'DO THE DOWNLOAD
		Try
			_Downloader = New WebFileDownloader
			_Downloader.DownloadFileWithProgress(txtURL.Text, txtDownloadTo.Text.TrimEnd("\"c) & GetFileNameFromURL(txtURL.Text))
		Catch ex As Exception
			MessageBox.Show("Error: " & ex.Message)
		End Try

	End Sub

	'returns all text from last "/" in URL, but puts a "\" in front of the file name..
	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

	'GET A FOLDER TO DOWNLOAD THE FILE TO
	Private Sub cmdGetFolder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdGetFolder.Click
		If dlgFolderBrowse.ShowDialog(Me) <> DialogResult.Cancel Then
			txtDownloadTo.Text = dlgFolderBrowse.SelectedPath
		End If
	End Sub

	'FIRES WHEN WE HAVE GOTTEN THE DOWNLOAD SIZE, SO WE KNOW WHAT BOUNDS TO GIVE THE PROGRESS BAR
	Private Sub _Downloader_FileDownloadSizeObtained(ByVal iFileSize As Long) Handles _Downloader.FileDownloadSizeObtained
		ProgBar.Value = 0
		ProgBar.Maximum = Convert.ToInt32(iFileSize)
	End Sub

	'FIRES WHEN DOWNLOAD IS COMPLETE
	Private Sub _Downloader_FileDownloadComplete() Handles _Downloader.FileDownloadComplete
		ProgBar.Value = ProgBar.Maximum
		MessageBox.Show("File Download Complete")
	End Sub

	'FIRES WHEN DOWNLOAD FAILES. PASSES IN EXCEPTION INFO
	Private Sub _Downloader_FileDownloadFailed(ByVal ex As System.Exception) Handles _Downloader.FileDownloadFailed
		MessageBox.Show("An error has occured during download: " & ex.Message)
	End Sub

	'FIRES WHEN MORE OF THE FILE HAS BEEN DOWNLOADED
	Private Sub _Downloader_AmountDownloadedChanged(ByVal iNewProgress As Long) Handles _Downloader.AmountDownloadedChanged
		ProgBar.Value = Convert.ToInt32(iNewProgress)
		lblProgress.Text = WebFileDownloader.FormatFileSize(iNewProgress) & " of " & WebFileDownloader.FormatFileSize(ProgBar.Maximum) & " downloaded"
		Application.DoEvents()
	End Sub

	Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		lblProgress.Text = String.Empty
		txtURL.Text = URL_MESSAGE
		txtDownloadTo.Text = DIR_MESSAGE
		Show()
		cmdGetFolder.Focus()
	End Sub

	Private Sub lnkMe_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles lnkMe.LinkClicked
		Process.Start("https://mvp.support.microsoft.com/default.aspx/profile=49C8B8DA-C5EF-42F4-8896-232F8A2A9FB4")
	End Sub

	Private Sub lnkForums_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles lnkForums.LinkClicked
		Process.Start("http://www.vbforums.com")
	End Sub
End Class



Is This A Good Question/Topic? 0
  • +

Page 1 of 1