Share via


Downloader Object

Represents the set of download functionality for a Silverlight plug-in. The downloader can asynchronously download content that can be obtained through an HTTP GET request.

XAML
Cannot be used in XAML.
Scripting
To create a Downloader object using scripting, see CreateObject.

Properties

DownloadProgress, Name, ResponseText, Status, StatusText, Uri

Methods

Abort, Equals, FindName, GetHost, GetResponseText, GetValue, Open, Send, SetValue

Events

Completed, DownloadFailed, DownloadProgressChanged

Remarks

The Downloader object is a special-purpose Silverlight object that provides the ability to download content, such as XAML content, JavaScript content, ZIP packages, or media assets, such as images. By using the Downloader object you do not have to provide all application content when the Silverlight plug-in is instantiated. Rather, you can download content on demand in response to application needs. More importantly, you can render downloaded content without the need to refresh the entire Web page. The Downloader object provides functionality for initiating the data transfer, monitoring the progress of the data transfer through events and status properties, and retrieving the downloaded content.

The properties and methods of the Downloader object are modeled after the XMLHttpRequest (XHR) set of APIs. XMLHttpRequest provides JavaScript and other web browser scripting languages the ability to transfer and manipulate XML data to and from a web server using HTTP.

Generally you do not specify the URI scheme of the Downloader source URI; you instead specify relative URIs, and use the URI scheme and site of origin that is being used by the host HTML page that contains the Silverlight plug-in. The Downloader cannot be used to download files across domains (content that is not from the site of origin for the hosting HTML page). For more information about Downloader URI restrictions and other aspects of using the Downloader object, see Using a Downloader Object.

Performance Tip   When using the Downloader object, after the Completed event fires, detach all event handlers from the Downloader and then set Downloader to null.

Examples

The following JavaScript example shows how to create, initialize, and execute a download request. This example hooks event handlers for two events ( Completed, DownloadProgressChanged), but these handlers are not shown in this example. Also, there is a third event DownloadFailed that you might want to write handlers for in some cases. See the complete example in the topic Using a Downloader Object.

JavaScript
// Event handler for initializing and executing a download request.
function onMouseLeftButtonUp(sender, eventArgs)
{
    // Retrieve a reference to the plug-in.
    var slPlugin = sender.getHost();
    // Create a Downloader object.
    var downloader = slPlugin.createObject("downloader");
    // Add DownloadProgressChanged and Completed events.
    downloader.addEventListener("downloadProgressChanged", onDownloadProgressChanged);
    downloader.addEventListener("completed", onCompleted);
    // Initialize the Downloader request.
    // NOTE: downloader APIs disallow file:\\ scheme
    // you must run this sample over localhost: or off a server or the following call will fail
    downloader.open("GET", "promo.png");
    // Execute the Downloader request.
    downloader.send();
}

See Also

Using a Downloader Object
CreateFromXamlDownloader
CreateObject
SetSource (Image)
SetSource (MediaElement)