Dan
Dan

Reputation: 1110

Transfer image over web request

I am working on a Xamarin.Forms PCL project and would like to transfer a photo that has been selected by the user over a web request

I am using the following code but don't know what to use for the image value, do I just

Image image = *selected image*
var values = new Dictionary<string, string>
        {
            {"user_session", session},
            {"image", ??? },
            . . . 
        };

        var content = new FormUrlEncodedContent(values);
        var webResponse= await App.client.PostAsync(App.URL + "test.php", content);
        var response= await webResponse.Content.ReadAsStringAsync();

Upvotes: 2

Views: 67

Answers (1)

Sajjad Abedi
Sajjad Abedi

Reputation: 122

you can convert your photo to string and then send it as a content of you Post method.

 MediaFile file; //add xam.plugin.media nuget package by jamesmontemagno

 var stream = file.GetStream();
            var bytes = new byte[stream.Length];
            await stream.ReadAsync(bytes, 0, (int)stream.Length);
            string content = System.Convert.ToBase64String(bytes);

you can also use xam.plugin.media package for picking/taking photo. read more Here.

Upvotes: 1

Related Questions