2

Sorry for my english. I have a method in which I send the StorageFile to the server. I tried using Windows.Web.Http.HttpClient, but does not work ( getting an invalid response from the server ) , so I use System.Net.Http.HttpClient.
Here is my code :

    public static async void Upload(string uri, StorageFile data, Action<double> progressCallback = null)
    {
        try
        {
            byte[] fileBytes =await ReadFile(data);
            System.Net.Http.HttpClient client = new System.Net.Http.HttpClient();
            MultipartContent content = new System.Net.Http.MultipartFormDataContent();
            var file1 = new ByteArrayContent(fileBytes);
            file1.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
            {
                Name = "file1",
                FileName = data.Name,
            };
            content.Add(file1);

            System.Net.Http.HttpResponseMessage response = await client.PostAsync(uri, content);
            response.EnsureSuccessStatusCode();

            var raw_response = await response.Content.ReadAsByteArrayAsync();
            var r2 = Encoding.UTF8.GetString(raw_response, 0, raw_response.Length);
            if (r2[0] == '\uFEFF')
            {
                r2 = r2.Substring(1);
            }
            Logger.Info(r2);
        }
        catch (Exception exc)
        {
            Logger.Error( exc);
        }
    }

Whether it is possible to change the code to receive progress about downloading a file in callback function?

croxy
  • 4,082
  • 9
  • 28
  • 46
Georgy
  • 97
  • 7

2 Answers2

0

On Windows Runtime you can try to switch to Windows.Web.HttpClient class. Its PostAsync method returns IAsyncOperationWithProgress<HttpResponseMessage, HttpProgress> interface. This interface has Progress event to which you can simply subscribe before awaiting the result.

Alex
  • 887
  • 1
  • 11
  • 29
  • Thanks for the answer! The problem is that when you use `Windows.Web.Http.HttpClient` I get an invalid response from the server. So I sometimes is used `Windows.Net.Http.HttpClient` – Georgy Jan 18 '16 at 17:31
  • If you want to use `System.Net.Http.HttpClient` then you need to implement your own `HttpMessageHandler` which will encapsulate progress logic and can be passed to `HttpClient` constructor. – Alex Jan 19 '16 at 16:13
0

Simplest way to upload file with progress

I had the same issue, and after some tries found out that you can easily get byte-accurate progress by tracking the Position of the FileStream of the file that you are going to upload.

This is a sample code that shows that.

FileStream fileToUpload = File.OpenRead(@"C:\test.mp3");

HttpContent content = new StreamContent(fileToUpload);
HttpRequestMessage msg = new HttpRequestMessage{
    Content=content,
    RequestUri = new Uri(--yourUploadURL--)
}

bool keepTracking = true; //to start and stop the tracking thread
new Task(new Action(() => { progressTracker(fileToUpload, ref keepTracking); })).Start();
var result = httpClient.SendAsync(msg).Result;
keepTracking = false; //stops the tracking thread

And define progressTracker() as

void progressTracker(FileStream streamToTrack, ref bool keepTracking)
{
    int prevPos = -1;
    while (keepTracking)
    {
        int pos = (int)Math.Round(100 * (streamToTrack.Position / (double)streamToTrack.Length));
        if (pos != prevPos)
        {
            Console.WriteLine(pos + "%");

        }
        prevPos = pos;

        Thread.Sleep(100); //only update progress every 100ms
    }
}

And this solved my problem.

Abraham
  • 12,140
  • 4
  • 56
  • 92