Sdílet prostřednictvím


Downloader

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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.

var downloader1 = silverlightObject.createObject("downloader");

Managed Equivalent

Downloader does not exist in the managed API. For most download tasks, you can use WebClient, or HttpWebRequest.

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 (for example, images). By using the Downloader object, you do not have to provide all application content when the Silverlight plug-in is instantiated. Instead, you can download content on demand in response to application needs. More importantly, you can render downloaded content after it is available, without the need to refresh the entire Web page. Downloader provides functionality for initiating the data transfer, monitoring the progress of the data transfer through events and status properties, and retrieving the downloaded content.

Generally you do not specify the Uniform Resource Identifier (URI) scheme of the Downloader source URI; instead, you 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.

NoteNote:

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

If you use ZIP file packages for downloader content, create these packages on a Microsoft Windows operating system. Silverlight does not support ZIP files created on Macintosh.

Example

The following JavaScript example shows how to create, initialize, and execute a download request. This example hooks event handlers for two events (Completed and 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.

// Create the 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();
}