xam.plugin.media - Upload to Server Error

DadaskiDadaski NGMember

Hi all,

I am trying to use xam.plugin.media by james montamagno version 2.6.2 to upload to a server but after about 1 minute, I get an abort with the message Task canceled. here is my code extract:

private async void uploadImage(Object sender, EventArgs e)
{
UserDatabase userDb = new UserDatabase();
User user = await userDb.returnLoggedInUser();
var content = new MultipartFormDataContent();
String Bearer = "Bearer " + user.token;

        HttpClientHandler handler = new HttpClientHandler();
        CookieContainer cookies = new CookieContainer();
        handler.CookieContainer = cookies;
        using (var httpClient = new HttpClient(handler, false))
        {

            httpClient.DefaultRequestHeaders.CacheControl = new CacheControlHeaderValue();
            httpClient.DefaultRequestHeaders.IfModifiedSince = new DateTime(2000, 3, 9, 16, 5, 7, 123);
            string reqWith = "Xamarin IOS/Android";
            httpClient.DefaultRequestHeaders.Add("X-Requested-With", reqWith);
            httpClient.DefaultRequestHeaders.Add("Authorization", Bearer);
            var uploadPath = "url goes here";
            try
            {
                var httpResponseMessage = await httpClient.PostAsync(uploadPath, content); //Stops at this line and displays  the error message after about a minute 
                remotePathLabel.Text = await httpResponseMessage.Content.ReadAsStringAsync();
            }
            catch (HttpRequestException ex)
            {
                Debug.WriteLine("ERRor " + ex.InnerException.Message);
            }
        }
    }

Any help will be appreciated. Thanks

Answers

  • NamyslawSzymaniukNamyslawSzymaniuk USMember ✭✭✭

    Lol, indeed it's NOT Xam.Plugin.Media issue.

    First of all, you can set longer timeout of httpclient:

    httpClient.Timeout = TimeSpan.FromSeconds(180); // 3 minutes

    But it might be API issue, or API-your mobile app connection issue.

    Try to do the same - upload MultiPartFile via Postman, with the same headers etc.

  • DadaskiDadaski NGMember

    @NamyslawSzymaniuk said:
    Lol, indeed it's NOT Xam.Plugin.Media issue.

    First of all, you can set longer timeout of httpclient:

    httpClient.Timeout = TimeSpan.FromSeconds(180); // 3 minutes

    But it might be API issue, or API-your mobile app connection issue.

    Try to do the same - upload MultiPartFile via Postman, with the same headers etc.

    Thanks @NamyslawSzymaniuk,

    This didnt work.... I just had to wait for longer. And Yes, I have tried uploading from the browser to the api and it was successful .

Sign In or Register to comment.