locked
how to get download percentage or progress from httpClient metro app c# ?

    Question

  • how can i archive this so that i can bind the download progress to a progress bar ? Please help i cant seem to get a way out .

    19karabo91

    Wednesday, August 27, 2014 1:29 PM

Answers

  • Ok, you should use "using Windows.Web.Http" instead of "using System.Net.Http".

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    • Proposed as answer by Dave SmitsMVP Wednesday, September 3, 2014 2:09 PM
    • Marked as answer by 19karabo91 Thursday, September 4, 2014 7:05 AM
    Wednesday, September 3, 2014 12:45 PM
    Moderator

All replies

  • Hi 19karabo91,

    Why not use BackgroundDownloader, a BackgroundDownloadProgress can be used to update progress bar.

    Anyway, HttpClient class can also get the download percentage, let's say for instance you download something by PostAsync | postAsync method, you should be able to get the Progress via IAsyncOperationWithProgress<HttpResponseMessage, HttpProgress> .

    Here you could see:

    Uri uri = new Uri("...");
                try
                {
                    HttpClient client = new HttpClient();
                    var downloadTask = client.GetAsync(uri);
    
                    downloadTask.Progress = (result, progress) => 
                    {
                        Debug.WriteLine("===start===");
                        Debug.WriteLine(progress.BytesReceived);
                        Debug.WriteLine(progress.TotalBytesToReceive);
                        Debug.WriteLine("===end===");
                    };
    
                    var Downloadresult = await downloadTask;
                    Debug.WriteLine("Done: " + Downloadresult.StatusCode.ToString());
                }
                catch (Exception ex)
                {
                }

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.


    Thursday, August 28, 2014 2:24 AM
    Moderator
  • Ive tried it but System.Threading.Task does not contain a definition for Progress and no methods for progress ?

    19karabo91

    Friday, August 29, 2014 12:07 PM
  • Ok, you should use "using Windows.Web.Http" instead of "using System.Net.Http".

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    • Proposed as answer by Dave SmitsMVP Wednesday, September 3, 2014 2:09 PM
    • Marked as answer by 19karabo91 Thursday, September 4, 2014 7:05 AM
    Wednesday, September 3, 2014 12:45 PM
    Moderator
  • Thanks Jamles  its working , but how do i bind the progress to a progressBar so it can show the download speed ?

    19karabo91

    Thursday, September 4, 2014 7:07 AM