Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 58056

Convert ImageSource to byte[] and send it to the API endpoint

$
0
0

Hi All,

My current task is to send image to the API endpoint, but I found it a little complicated so thanks in advance for your help.

Sample cURL for my endpoint looks like this:

curl -H "Content-type: multipart/form-data" -F file=@IMG_0047.JPG -F authentication_token=PsDUTQo2s5eLCt1osavT http://localhost:3000/cover_photo

so in my View I have got a Binding to the ViewModel which has property as follow:

Image coverImage = new Image
{
Aspect = Aspect.AspectFill,
};

coverImage.SetBinding(Image.SourceProperty, "ImageTaken");

private ImageSource imageTaken;
        public const string ImageTakenPropertyName = "ImageTaken";
        public ImageSource ImageTaken
        {
            get { return imageTaken; }
            set { SetProperty(ref imageTaken, value, ImageTakenPropertyName); }
        }

And here everything is ok, I am taking a photo and it is binded to the Image control. Next in my ViewModel I am calling:

var isFileUploaded = await _questRepository.SendImage(ImageTaken.GetByteArray());

where GetByteArray() is an extension method where I don't know how to convert ImageSource to byte[]:

`public static byte[] GetByteArray(this ImageSource imageSource)
    {
        Byte[] buffer = null;

            return buffer;
    }`

To be next able to send it like this:

`public async Task SendImage(byte[] photoBytes)
{
bool result = false;

    using (var client = new HttpClient(new NativeMessageHandler()))
    {
        client.DefaultRequestHeaders.Authorization =
            new System.Net.Http.Headers.AuthenticationHeaderValue(
            StaticData.ApiUsername,
            StaticData.ApiPassword
        );

        Uri requestUri = new Uri(
            string.Concat(
                StaticData.ApiEndpoint,
                "/cover_photo"
            )
        );

        var content = new MultipartFormDataContent();

        var fileContent = new ByteArrayContent(photoBytes);

        fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse("multipart/form-data");
        fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
        {
            FileName = "image.jpg",
            Name = "file"
        };

        content.Add(fileContent);

        HttpResponseMessage response = client.PostAsync(
            requestUri,
            content
        ).Result;

        if (response.IsSuccessStatusCode)
        {
            Task<string> con = response.Content.ReadAsStringAsync();

            result = true;
        }
    }

    return result;
}`

And the second question is how to fill FIleName and Name property of ContentDispositionHeaderValue from ImageSource.

Thank you for your time.


Viewing all articles
Browse latest Browse all 58056

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>