Using a WebClient to download a text file

The DownloadStringAsync() function of the WebClient class can be used to download the contents of files as a string. The DownloadStringAsync() accepts a Uri object as an argument and sends the equivalent of a GET request to the server to retrieve the content of the file specified by the Uri . For example:

client.DownloadStringAsync(new Uri("afile.txt", UriKind. Relative));

To access the result of the DownloadStringAsync() function, you need to attach a DownloadStringCompleted event handler function to the WebClient object. For example, the following line of code attaches a DownloadStringCompleted event handler function doDownloadCompleted() to a WebClient object named client.

client.DownloadStringCompleted +=

new DownloadStringCompletedEventHandler(doDownloadCompleted);

The DownloadStringCompleted event handler function must accept an object, the sender, as the first argument and a DownloadStringCompletedEventArgs as the second argument.

Inside the DownloadStringCompleted event handler function, the contents of the file downloaded can be accessed using the result property of the DownloadStringCompleted EventArgs argument. The result property is of string type and contains the contents of the downloaded file.

Listings 14.3 and 14.4 show a simple example of using a WebClient object to download a text file and display it in a Silverlight application. The code in Listing 14.3 defines a simple interface with a Button control to initiate a file download and a TextBlock in a ScrollViewer to display the contents of the downloaded file.

LISTING 14.3

Page.xaml File That Defines a Button Control to Initiate a File Download and a TextBlock Control in a ScrollViewer to Display the Results

<UserControl x:Class="DownloadStringApp.Page"

xmlns="http://schemas.microsoft.com/client/2 0 07" xmlns:x="http://schemas.microsoft.com/winfx/2 00 6/xaml" Width="400" Height="3 00">

<Grid x:Name="LayoutRoot" Background="White">

<ScrollViewer HorizontalScrollBarVisibility="Auto"

LISTING 14.3

(continued)

Height="200" Width="3 00" VerticalAlignment="Top" Margin=M20M> <TextBlock x:Name=MdownloadTextM/> </ScrollViewer>

<Button x:Name=MdownloadBtnM Content=MDownload Dynamic Content" VerticalAlignment="Bottom" Margin="2 0" Height="3 0" Width=M2 00M />

The code in Listing 14.4 implements the System.Net library and then attaches a Click event handler, doGetContent() , to the Button control defined in Listing 14.3. Inside the doGet-Content() event handler, the WebClient object, client, is created.

A DownloadStringCompleted event handler, doDownloadCompleted() , is attached to the client object. Then the following line of code is used to create a Uri object for the readme.txt file and initiate the file download:

client.DownloadStringAsync(new Uri(filename, UriKind.Relative));

Inside the handler doDownloadCompleted() function, the result property of the DownloadStringCompletedEventArgs argument is used to set the Text property of the downloadText TextBlock defined in Listing 14.3.

LISTING 14.4

Page.xaml.cs File That Creates a WebClient and Uses It to Download the Contents of a File Named readme.txt using System;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Input;

using System.Net;

namespace DownloadStringApp {

public partial class Page : UserControl

InitializeComponent();

downloadBtn.Click += new RoutedEventHandler(doGetContent);

void doGetContent(object sender, RoutedEventArgs e) {

string filename = "readme.txt";

WebClient client = new WebClient(); client.DownloadStringCompleted +=

new DownloadStringCompletedEventHandler(doDownloadCompleted); client.DownloadStringAsync(new Uri(filename, UriKind.Relative));

void doDownloadCompleted(object sender,

DownloadStringCompletedEventArgs e)

downloadText.Text = e.Result;

The results of the application defined by Listings 14.3 and 14.4 are shown in Figure 14.1. When the user clicks Download Dynamic Content, the contents of the file are displayed in the text block.

FIGURE 14.1

Silverlight application that dynamically downloads a text file and displays it

This method retrieves the specified resi^i The resource is downloaded asynchron« the DownloadStringCompleted event is in the Result property of the Download? You cannot cafl the DownloadStrirgAsy until the first string download operatioi

You can use the CancelAsyrc method tw

Download Dynamic Content jJ

+1 0

Average user rating: 5 stars out of 1 votes

Post a comment

  • Receive news updates via email from this site