Downloading a file or a pricture by clicking on a button RRS feed

  • Question

  • i am using visual studio 2013 i cant seem to find a tutorial on how to make a downloader or just a way to download a file through a link ...  any help? 
    Friday, April 10, 2015 8:26 PM


All replies

  • The Windows-Apps-API contains a very interesting class called HttpClient which gives you a lot of different functions to work with.

    Here you have your "tutorial" for how to use this Client.

    © 2015 Thomas Roskop

    Germany // Deutschland

    Friday, April 10, 2015 8:38 PM
  • Hi Jalal natshes,

    You can use Downloads the resource with the specified URI to a local file.

    WebClient.DownloadFileAsync(String,String ).

    For eg-

    private void btnDownloadFile_Click(object sender, EventArgs e) {

    //URL where your images/files are located string url = @"http://www.xyz.com/wp-content/images/myimage.png"; // Create an instance of WebClient WebClient client = new WebClient(); // DownloadFileCompleted Event client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted); // Start the download and copy the file to c:\Downloadedfiles folder client.DownloadFileAsync(new Uri(url), @"c:\Downloadedfiles\myimage.png"); } void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e) { MessageBox.Show("File downloaded successfully"); }

    For More Details find following link:-


    Don't forget to mark the right answer and vote up if helps you.

    • Edited by Siddhanath Saturday, April 11, 2015 3:47 AM
    Saturday, April 11, 2015 3:45 AM
  • Siddhanath, your code is not valid for Windows Store apps. It will work (but not ideally) only for desktop apps.

    Jalal, please take a look at the How to download a file documentation.

    Saturday, April 11, 2015 3:59 AM
  • Here is an example of how you could use the HttpClient class and streams to download a file from a URL and save into the local folder (https://msdn.microsoft.com/en-us/library/windows/apps/windows.storage.applicationdata.localfolder) of your application:

     <Button Content="Download file" Click="Button_Click"/>

    private async void Button_Click(object sender, RoutedEventArgs e)
                Uri uri = new Uri("http://www.yoursite.com/yourpic.png");
                StorageFolder localFolder = ApplicationData.Current.LocalFolder;
                string filename = "pic.png";
                StorageFile posterFile = await localFolder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);
                Windows.Web.Http.HttpClient httpClient = new Windows.Web.Http.HttpClient();
                var image = await httpClient.GetAsync(uri);
                using (Windows.Storage.Streams.IInputStream inputStream = await image.Content.ReadAsInputStreamAsync())
                    using (Windows.Storage.Streams.IOutputStream outputStream = await posterFile.OpenAsync(FileAccessMode.ReadWrite))
                        await Windows.Storage.Streams.RandomAccessStream.CopyAndCloseAsync(inputStream, outputStream);

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then please start a new thread if you have a new question. Please don't ask several questions in the same thread.

    Saturday, April 11, 2015 8:48 AM