Click here to Skip to main content
14,694,402 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I would like to upload an image from my project to my web service.

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


Thanks,
Jassim

What I have tried:

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.Clear();
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);
Posted
Updated 13-Sep-19 5:46am

1 solution

You can't use FormUrlEncodedContent to upload a file; you have to use MultipartFormDataContent.
var content = new MultipartFormDataContent();
var imageContent = new StreamContent(await imageEditor.GetStream());
content.Add(imageContent, "image");

var response = await client.PostAsync("https://my.domain.com/api/save.php", content);
   
Comments
Jassim Rahma 13-Sep-19 22:07pm
   
but I need to pass more than one parameter like this:

var content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string="">("student", Convert.ToString(student_number)),
new KeyValuePair<string, string="">("photo", STUDENT_PHOTO),
});

so I should pass it as a string?
Richard Deeming 16-Sep-19 5:18am
   
You cannot use FormUrlEncodedContent to upload a file.
var content = new MultipartFormDataContent();

// Add the image:
var imageContent = new StreamContent(await imageEditor.GetStream());
content.Add(imageContent, "photo");

// Add another parameter:
content.Add(new StringContent(Convert.ToString(student_number)), "\"student\"");
Jassim Rahma 22-Sep-19 5:26am
   
Yes that is Great. Thanks.
Jassim Rahma 22-Sep-19 8:59am
   
I have one more question Richard.

I am trying to pass more than one data in addition to the file but it;s not sending anything although my PHP backend works fine when I test

my PHP expect:


$photo = $_FILES["photo"]["tmp_name"];
$user = $_POST["user"];


but now I want to add:


$ad = $_POST["ad"];


so it will be:


$ad = $_POST["ad"];
$photo = $_FILES["photo"]["tmp_name"];
$user = $_POST["user"];


here is what I tried but not working:


var content = new MultipartFormDataContent();

// Add the image:
var imageContent = new StreamContent(await imageEditor.GetStream());
imageContent.Headers.ContentType = new MediaTypeHeaderValue("image/png");
content.Add(imageContent, "photo", "AdImage.png");

// Add another parameter:
content.Add(new StringContent(ad_id), "\"ad\"");
content.Add(new StringContent(App.getUserInfo("MyUserID").Result), "\"user\"");
Jassim Rahma 22-Sep-19 10:12am
   
sorry it's ok now, it was a problem with the backend PHP

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900