Visual Basic Code Snippet - Download File from URL

Visual Basic Code Snippet - Download File from URL

Visual Basic Code Snippet - Download File from URL

(VB) Visual Basic code snippet download file from URL and save it on local drive. These code snippets discuss both asynchronous file download method and DownloadFile method (to block thread while waiting for the download).

Bookmark:

Visual Basic Code Snippet - Download File from URL

This .Net Visual Basic code snippet download file from URL to local disk. To use this function simply provide the URL of the file and file name to save on local drive. This function uses System.Net namespace to download the file.

''' <summary>
''' Function to download a file from URL and save it to local drive
''' </summary>
''' <param name="_URL">URL address to download file</param>
Public Sub DownloadFile(ByVal _URL As String, ByVal _SaveAs As String)
    Try
        Dim _WebClient As New System.Net.WebClient()
        ' Downloads the resource with the specified URI to a local file.
        _WebClient.DownloadFile(_URL, _SaveAs)
    Catch _Exception As Exception
        ' Error
        Console.WriteLine("Exception caught in process: {0}", _Exception.ToString())
    End Try
End Sub


Here is a simple example showing how to use above function (DownloadFile) to download a file and save it on a local disk.

// Download file from URL
DownloadFile("http://www.yourdomain.com/sample-file.zip", "C:\\sample-file.zip")


Download File Asynchronously
To download a file without blocking main calling thread, Downloads, to a local file, the resource with the specified URI. This method does not block the calling thread.

To receive notification when the file is available, add an event handler to the DownloadFileCompleted event. This event occurs when an asynchronous file download operation completes. To update status/progress bar while downloading file use DownloadProgressChanged event. This event occurs when an asynchronous download operation successfully transfers some or all of the data. You can use the CancelAsync method to cancel asynchronous operations that have not completed.

To block while waiting for the download to complete, use one of the DownloadFile (discuss in above) methods.

' Occurs when an asynchronous file download operation completes.
Private Sub _DownloadFileCompleted(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs)
    ' File download completed
    download_button.Enabled = Enabled
    MessageBox.Show("Download completed")
End Sub

' Occurs when an asynchronous download operation successfully transfers some or all of the data.
Private Sub _DownloadProgressChanged(ByVal sender As Object, ByVal e As System.Net.DownloadProgressChangedEventArgs)
    ' Update progress bar
    progressBar1.Value = e.ProgressPercentage
End Sub


' download button click event
Private Sub download_button_Click(ByVal sender As Object, ByVal e As EventArgs) Handles download_button.Click
    ' Disable download button to avoid clicking again while downloading the file
    download_button.Enabled = False

    ' Downloads, to a local file, the resource with the specified URI. 
    ' This method does not block the calling thread.
    Dim _WebClient As New System.Net.WebClient()
    AddHandler _WebClient.DownloadFileCompleted, AddressOf _DownloadFileCompleted
    AddHandler _WebClient.DownloadProgressChanged, AddressOf _DownloadProgressChanged
    _WebClient.DownloadFileAsync(New Uri("http://www.youdomain.com/sample-file.zip"), "C:\\sample-file.zip")
End Sub


VB Keywords Used:

  • WebClient
  • DownloadFile
  • DownloadFileCompleted
  • DownloadProgressChanged
  • DownloadFileAsync
  • Exception

Code Snippet Information:

  • Applies To: .Net, VB, Visual Basic, CLI, Download file, WebClient, DownloadFile, DownloadFileAsync, DownloadFileCompleted, DownloadProgressChanged, Asynchronous file download
  • Programming Language : Visual Basic (VB)

External Resources:

Leave a comment