Share via


Xamarin: Download a file with ProgressBar

This recipe shows how to download a file using WebClient in Xamarin.Android.

To download a file (.mp3, etc.) follow these steps:

  • Add new using statements to your code:

    using Java.IO;
    using System.IO;
    using System.Net;
    
  • Create a WebClient object:

    var webClient=new WebClient();
    
  • Create the URL for the files to download:

    var url=new Uri("http://8morning.com/demo/wp-content/uploads/2016/04/hoc-photoshop.jpg");
    
  • Create the array for contains the length of a file.

    byte[] bytes=null;
    

    Add an event handler that will execute when the download is complete. Create a ProgressDialog to show a progress indicator and an optional text message.

    webClient.DownloadProgressChanged+= WebClient_DownloadProgressChanged;
    
    
                dialog = new ProgressDialog (this);
                dialog.SetProgressStyle (ProgressDialogStyle.Horizontal);// style is horizontal
                dialog.SetTitle("Downloading...");
                dialog.SetCancelable (false);
                //dialog.SetButton("Cancel",);
                dialog.SetCanceledOnTouchOutside (false); //if it is true. When you touch anywhere lie outside the dialog-> dialog will be close.
                dialog.Show ();// showing a dialog
     
                try
                {
                    bytes= await webClient.DownloadDataTaskAsync(url);
                }
                catch(TaskCanceledException)
                {
                    Toast.MakeText(this,"Task Canceled!",ToastLength.Long).Show();
                    return;
                }
                catch(Exception a)
                {
                    Toast.MakeText(this,a.InnerException.Message,ToastLength.Long).Show();
                    dialog.Progress=0;
                    return;
                }
    
    var documentsPath=Android.OS.Environment.ExternalStorageDirectory+"/Download"; // The direction is Download folder in Device store. A file will be save here.
                string localFilename = Name your file +".jpg";
                string localPath=System.IO.Path.Combine(documentsPath,localFilename);// documentsPath +"/"+ localFilename
                
     
                dialog.SetTitle("Download Complete");//
     
                //Save the Mp3 using writeAsync
                FileStream fs=new FileStream(localPath,FileMode.OpenOrCreate);
                
                await fs.WriteAsync (bytes, 0, bytes.Length);// writes to Download folder 
                fs.Close ();
     
     
                dialog.Progress = 0;
    
  • In event`` ``WebClient_DownloadProgressChanged will execute progress when a file is downloading.

    void WebClient_DownloadProgressChanged (object sender, DownloadProgressChangedEventArgs e)
            {
                dialog.Progress = e.ProgressPercentage;
                if (e.ProgressPercentage == 100) {
                    dialog.Hide ();
                }
            }
    

Sample

-