Upload Image using PostAsync RRS feed

  • Question

  • Hi,

    I would like to upload an image from my project to my web service. Here is my code:

    byte[] fileBytes;
    using (var fileStream = await imageEditor.GetStream())
        var binaryReader = new BinaryReader(fileStream);
        fileBytes = binaryReader.ReadBytes((int)fileStream.Length);
    var imageBinaryContent = new ByteArrayContent(fileBytes);
    var multipartContent = new MultipartFormDataContent();
    multipartContent.Add(imageBinaryContent, "image");

    and my PostAsync is like this:

    var client = new HttpClient();
    client.BaseAddress = new Uri("https://my.domain.com/api/save_data.php");
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    var content = new FormUrlEncodedContent(new[]
        new KeyValuePair<string, string>("image", IMAGE_DATA_COMES_HERE)
    var response = await client.PostAsync("https://my.domain.com/api/save.php", content);

    How can I put the image into my PostAsync in order to upload to my PHP web service?



    Wednesday, September 11, 2019 11:35 PM

All replies

  • Hi Jassim Rahma,

    Thank you for posting here.

    According to your question, I have found two related references.

    1. Upload image using HttpClient
    2. Upload an image via httpclient

    Hope them can help you.

    Best Regards,

    Xingyu Zhao

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, September 12, 2019 1:47 AM