Forum Xamarin.Forms

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

web api image + json in one go. WHATS THE PROPER WAY?

so this is my code for posting

public async Task AddProduct(int productid, string productCategory, string productColor, decimal unitPrice, string userId,byte[]image)
{

        var client = new HttpClient();

        var model = new Products
        {
            ProductId = productid,
            ProductCategory = productCategory,
            ProductColor = productColor,
           UnitPrice = unitPrice,
            UserId = userId,
            // Image = image <<<< DO I ADD THIS?






        };

        var json = JsonConvert.SerializeObject(model);

      var content = new StringContent(json);
        content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

        var response = await client.PostAsync("http://192.168.1.6:44331/api/Products", content);

        return response.IsSuccessStatusCode;

    }

My question is this. and I'm really so confused about this part. in my view there will be add product. thats looks something like this

StackLayout>

    <Entry Text="{Binding ProductColor}" Placeholder="Enter Product Color"/>
    <Entry Text="{Binding ProductCategory}" Placeholder="Enter Product Category"/>
    <Entry Text="{Binding UnitPrice}" Placeholder="Enter Product Cost" Keyboard="Numeric"/>
    <Button Command="{Bingind AddProductCommand}" Text="Add Product"/>





</StackLayout>

in my view model i am just calling that service to a command to add the product. So my question is how can i slid the image there and post it in one go.
Or at least upload the image to azure blob storage and return it as uri then post it with a single command. Give me an idea on how to do that please. I've been reading so much topic about this. and all they teach is how to upload the image without the data along with it

Tagged:

Answers

  • jezhjezh Member, Xamarin Team Xamurai

    in my view model i am just calling that service to a command to add the product. So my question is how can i slid the image there and post it in one go.
    Or at least upload the image to azure blob storage and return it as uri then post it with a single command.

    Do you want to upload your image to your back server or azure blob storage?

    If you want to upload your images to your backend server, you need to communicate with the backend developer how the data will interact (including images). When the image is uploaded successfully, you can get the URL of the image. The next time you want to display it, you can use this URL to display the image.For how to display image, you can check: https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/images?tabs=windows#display-images

    If you want to upload your image to azure blob storage,you can check the official document:https://docs.microsoft.com/en-us/azure/storage/blobs/storage-upload-process-images?tabs=dotnet
    For more details, you can check: https://docs.microsoft.com/en-us/azure/storage/blobs/storage-blobs-introduction

  • tomrizardo23tomrizardo23 Member ✭✭
    Thank you for answering. I wanna try backend first before doing blob. So how will i bind them together. When i upload the images how will i make some kind of relationship to json object
  • jezhjezh Member, Xamarin Team Xamurai

    So how will i bind them together. When i upload the images how will i make some kind of relationship to json object

    For this question, you should communicate with the background developer and discuss the interface for data interaction(including how to upload your images) with background developers.

Sign In or Register to comment.