ARTICLE

Download Content From Internet Location in Windows Store Apps

Posted by Gaurav Gupta Articles | Windows Store Apps September 27, 2012
In this article we will learn how to download a file using the Background Transfer API from a network location.
Reader Level:

Today we will learn about the Background Transfer task in Windows Store Apps for downloading content from the Internet. The user can request files from the locations on the Internet. 

To access this feature you must include Background Transfer ability in your application. Windows Store apps provides an API for Background Transfer downloading called Background Transfer. Unlike the HTTP APIs, Background Transfer can be used by any language supported by the Windows Runtime.

In this article I will show you how to add Background Transfer Downloading feature in your Windows Store application Using C#/XAML. To download the file from the Internet here I use BackgroundDownloader Class.

Here are the instructions to be followed.

Step 1

Create a Blank Windows Store App using C#.

Step 2

To access the network in your application, you have to ensure that your app has the capability to access the internet. To add the capability use these steps:
 

  • In the Solution Explorer go to the Pacakage.appxmanifest.
  • Select the Capabilities tab from the top.
  • Check the Internet Checkbox and Picture Library.

     
Background-transfer-in-windows-store-apps.jpg 

Step 3

In this step I design the UI of the page using XAML.

<Page

    x:Class="DownloadAFile.MainPage"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:local="using:DownloadAFile"

    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

    mc:Ignorable="d">

 

    <Grid>

        <Grid.Background>

            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">

                <GradientStop Color="Black"/>

                <GradientStop Color="White" Offset="1"/>

            </LinearGradientBrush>

        </Grid.Background>

        <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">       

            <StackPanel Orientation="Horizontal" Margin="0,10,0,0">

                <TextBlock Text="Local file name: " FontSize="25" Margin="0,8,17,0" />

                <TextBox x:Name="fileNameField" Text="SampleImage.png" Width="200"/>

            </StackPanel>

            <StackPanel Orientation="Vertical" Margin="0,30,0,0">

                <Button x:Name="StartDownloadButton" Content="Start Download" Margin="80,0,10,0" Click="StartDownload_Click"/>

                <TextBlock x:Name="Msg" Margin="0,8,17,0" FontSize="20"  />

                <ProgressRing x:Name="Progress" Width="100" Height="50" Foreground="Red"></ProgressRing>

            </StackPanel>

        </StackPanel>

    </Grid>

</Page>


Step 4

Here is the coding of .cs file.

First of all add the namespaces to be used for the classes in the application they are:

using Windows.Networking.BackgroundTransfer;

using Windows.Storage;

Now, I create the source URI from which we want to download and the destination location where we want to store to; see:

Uri source;

if(!Uri.TryCreate("http://4.bp.blogspot.com/_uAGqOSyGDCM/TPZzGcbsdlI/AAAAAAAAAYE/
9qoly4JXPV4/s1600/evil+gogle.jpg"
,UriKind.Absolute, out source))

{            

}        

string destination = fileNameField.Text.Trim();         

StorageFile destinationFile = null; ;

try

{

    destinationFile = await KnownFolders.PicturesLibrary.CreateFileAsync(

    destination, CreationCollisionOption.GenerateUniqueName);
}

catch (FileNotFoundException ex)

{            

}

Here I created a new DownloadOperation object using the BackgroundDownloader class when passed the required StorageFile and the URI values that we created recently and call the CreateDownload method by passing two arguments; see:
 

BackgroundDownloader downloader = new BackgroundDownloader();

DownloadOperation download = downloader.CreateDownload(source, destinationFile);
await HandleDownloadAsync(download, true);

With the download configured, HandleDownloadAsync is called to attach progress and completion handlers to the operation before initiating the download; see:

private async Task HandleDownloadAsync(DownloadOperation download, bool start)

{

   try

   {

     if (start)

     {

          // Start the download and attach a progress handler.

        await download.StartAsync();
     }

    ResponseInformation response = download.GetResponseInformation();

    Msg.Text = "File is Successfully Downloaded";

   }

  catch

  {

   Msg.Text = "Problem Occurs in Doownloading";

  }
}

Step 5

Now the app is ready for downloading content from the Internet. Press F5 and run it.

Click on the Start Download button.

Download-File-in-Windows-Store-apps.jpg

Background-transfer-download-in-windows-store-apps.jpg

COMMENT USING

Trending up