C# Asynchronous Download File with Progress

// Sample call : DownLoadFileInBackground2 ("http://www.contoso.com/logs/January.txt");

public static void DownLoadFileInBackground2(string address)
{
    WebClient client = new WebClient ();
    Uri uri = new Uri(address);

    // Specify that the DownloadFileCallback method gets called
    // when the download completes.
    client.DownloadFileCompleted += new AsyncCompletedEventHandler (DownloadFileCallback2);

    // Specify a progress notification handler.
    client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadProgressCallback);
    client.DownloadFileAsync (uri, "serverdata.txt");

}
private static void UploadProgressCallback(object sender, UploadProgressChangedEventArgs e)
{

    // Displays the operation identifier, and the transfer progress.
    Console.WriteLine("{0}    uploaded {1} of {2} bytes. {3} % complete...", 
        (string)e.UserState, 
        e.BytesSent, 
        e.TotalBytesToSend,
        e.ProgressPercentage);

}
 

private static void DownloadProgressCallback(object sender, DownloadProgressChangedEventArgs e)
{    
     // Displays the operation identifier, and the transfer progress.    
     Console.WriteLine("{0}    downloaded {1} of {2} bytes. {3} % complete...",         (string)e.UserState,         
        e.BytesReceived,         e.TotalBytesToReceive,                    e.ProgressPercentage);

}

Source: https://msdn.microsoft.com/en-us/library/ms144196(v=vs.110).aspx

Leave a Reply