Download a File

PDF for offline use
Sample Code:
Related Articles:

Let us know how you feel about this

Translation Quality


0/250

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

Recipe

To download a text file follow these steps:

  • Add a new using statements to your code:
using System.Net;
using System.IO;
using System.Text;
  • Create a WebClient object:
var webClient = new WebClient();
  • Add an event handler that will execute when the download is complete. First it retrieves the downloaded text to a variable, then it creates a local file path and finally it saves the file to local storage:
webClient.DownloadStringCompleted += (s, e) => {
    var text = e.Result; // get the downloaded text
    string documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
    string localFilename = "downloaded.txt";
    string localPath = Path.Combine (documentsPath, localFilename);
    File.WriteAllText (localpath, text); // writes to local storage
};
  • Create the Url for the file to download:
var url = new Uri("http://xamarin.com"); // Html home page
  • Set the encoding to match the expected encoding of the file”
webClient.Encoding = Encoding.UTF8;
  • Finally trigger the download itself (when complete, the handler we defined in step 3 will execute):
webClient.DownloadStringAsync(url);

Additional Information

The sample code looks like this screenshot when the file has been successfully downloaded. The alert and text view are populated by adding the following code to the completion handler defined in Step 3:

InvokeOnMainThread (() => {
    textView.Text = text;
    new UIAlertView ("Done", "File downloaded and saved", null, "OK", null).Show();
});

Notice that all interactions with UI controls is done within an InvokeOnMainThread method call. This is because the file download occurs asynchronously (in a background thread), and background threads aren’t allowed to access the UI directly.

Xamarin Workbook

If it's not already installed, install the Xamarin Workbooks app first. The workbook file should download automatically, but if it doesn't, just click to start the workbook download manually.