Download image and string over HTTP POST windows phone 8.1 using HttpClient

I have a Windows Phone application in C #. I am trying to send an image (byte []) and session token (string) to my django server, but not how.

I looked at another post but it doesn't work what they do or the classes used don't exist.

My function header:

    public static async Task<bool> sendImagePerfil(string token, byte[] imagen)
    {
        using (var client = new HttpClient())
        {
            var values = new List<KeyValuePair<string, string>>();
            values.Add(new KeyValuePair<string, string>("token", token));
            values.Add(new KeyValuePair<string, string>("image", Convert.ToString(imagen)));

            var content = new FormUrlEncodedContent(values);

            var response = await client.PostAsync("MyURL.domain/function", content);

            var responseString = await response.Content.ReadAsStringAsync();
        }


    }

      

EDITED: My problem now is that my server is not receiving the image. Django code:

     if request.method == 'POST':
        form = RestrictedFileField(request.POST, request.FILES)
        token = models.UsuarioHasToken.objects.get(token=parameters['token'])
        user = token.user
        print (request.FILES['image'])
        user.image = request.FILES['image']

      

I cant change django code because this code it works with android app

+3


source to share


1 answer


Using this answer,

How to upload file to server using HTTP POST multipart / form-data



Try with this ...

        HttpClient httpClient = new HttpClient();
        MultipartFormDataContent form = new MultipartFormDataContent();

        form.Add(new StringContent(token), "token");

        var imageForm = new ByteArrayContent(imagen, 0, imagen.Count());
        imagenForm.Headers.ContentType = new MediaTypeHeaderValue("image/jpg");

        form.Add(imagenForm, "image", "nameholder.jpg");

        HttpResponseMessage response = await httpClient.PostAsync("your_url_here", form);

        response.EnsureSuccessStatusCode();
        httpClient.Dispose();
        string result = response.Content.ReadAsStringAsync().Result;

      

+7


source







All Articles