C++/CLI Code Snippet - Download File from URL

C++/CLI Code Snippet - Download File from URL

C++/CLI Code Snippet - Download File from URL

C++/CLI 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:

C++/CLI Code Snippet - Download File from URL

This .Net C++/CLI 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>
void DownloadFile(System::String ^_URL, System::String ^_SaveAs)
{
    try
    {
        System::Net::WebClient ^_WebClient = gcnew System::Net::WebClient();
        // Downloads the resource with the specified URI to a local file.
        _WebClient->DownloadFile(_URL, _SaveAs);
    }
    catch (Exception ^_Exception)
    {
        // Error
        Console::WriteLine("Exception caught in process: {0}", _Exception->ToString());
    }
}


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.
void Form1::_DownloadFileCompleted(System::Object ^sender, AsyncCompletedEventArgs ^e)
{
    // File download completed
    download_button->Enabled = Enabled;
    MessageBox::Show("Download completed");
}

// Occurs when an asynchronous download operation successfully transfers some or all of the data.
void Form1::_DownloadProgressChanged(System::Object ^sender, System::Net::DownloadProgressChangedEventArgs ^e)
{
    // Update progress bar
    progressBar1->Value = e->ProgressPercentage;
}


// download button click event
private: System::Void download_button_Click(System::Object^  sender, System::EventArgs^  e) {
    // 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.
    System::Net::WebClient ^_WebClient = gcnew System::Net::WebClient();
    _WebClient->DownloadFileCompleted += gcnew AsyncCompletedEventHandler(this, &Form1::_DownloadFileCompleted);
    _WebClient->DownloadProgressChanged += gcnew System::Net::DownloadProgressChangedEventHandler(this, &Form1::_DownloadProgressChanged);
    _WebClient->DownloadFileAsync(gcnew Uri("http://www.google.com/intl/en_ALL/images/logo.gif"), "C:\\SampleImage.gif");
}


C++/CLI Keywords Used:

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

Code Snippet Information:

  • Applies To: .Net, C++, CLI, Download file, WebClient, DownloadFile, DownloadFileAsync, DownloadFileCompleted, DownloadProgressChanged, Asynchronous file download
  • Programming Language : C++/CLI

External Resources:

Seph :: December 21-2010 :: 11:46 PM

Hello. Thank you for this really helpful information! Just got my autoupdater to work! :) But I got one question. How can I show download speed/estimated time/File size? Thanks for the help. :)

Leave a comment