0

I use xamarin forms to make multiplatforms applications, I want to upload picture in a server but when I upload all pictures my application is very slowed.

I use the WebClient to make request. there is my upload function:

public static async Task<string> CreateUploadTask(string File, string vtour_id, string user_id, string user_login)
        {
            string requestResult = "";
            using (WebClient client = new WebClient())
            {
                // add event listeners
                client.UploadProgressChanged += Client_UploadProgressChanged;

                // set the file type
                client.Headers.Add("Content-Type", "image/jpeg");
                // upload the file
                byte[] response = await client.UploadFileTaskAsync(Database.URL_DATABASE + "uploadPicture", "POST", File);

                // delete the event listeners
                client.UploadProgressChanged -= Client_UploadProgressChanged;

                requestResult = client.Encoding.GetString(response);
                client.Dispose();
            }

            return requestResult;
        }

After that the navigation is very slowed.

how can I improve that ?

thanks!

rem
  • 161
  • 1
  • 12
  • 1
    Probably unrelated, but why do you `client.Dispose();` - that's what `using` is doing anyway. You should actually get a warning about that somewhere. – Fildor Jul 19 '19 at 09:12
  • I think the problem is in the code calling this, not this method itself. There _could_ be a problem with creating new WebClient all the time, if it in any way behaves similar to HttpClient. – Fildor Jul 19 '19 at 09:13
  • 2
    If `WebClient`is [System.Net.WebClient](https://learn.microsoft.com/en-us/dotnet/api/system.net.webclient?view=netframework-4.8), then mind the remark: _"We don't recommend that you use the WebClient class for new development. Instead, use the System.Net.Http.HttpClient class."_ And if you follow that suggestion, you certainly don't want to create a new HttpClient for each Upload. (google "You are using HttpClient wrong") – Fildor Jul 19 '19 at 09:16

0 Answers0