0

i'm trying to send a photo to a server using httpclient class but every time i try i get a 0 byte file , here's my code for sending the image

if (e.ChosenPhoto != null)
                    {
                        var fileUploadUrl = Globals.baseUrl + "/laravelProjects/VisWall/public/test2";
                        var client = new HttpClient();
                        photoStream.Position = 0;
                        MultipartFormDataContent content = new MultipartFormDataContent();
                        content.Add(new StreamContent(e.ChosenPhoto), "image", fileName);
                        HttpResponseMessage result = new HttpResponseMessage();
                        await client.PostAsync(fileUploadUrl, content).ContinueWith((postTask) =>
                        {
                            try
                            {
                                result = postTask.Result.EnsureSuccessStatusCode();
                            }
                            catch (Exception exc)
                            {
                                MessageBox.Show("errorrrrrr");
                            }
                        });
                     }

i've also checked the length of e.ChoosenPhoto and it's not 0

1 Answers1

0

try this piece of code by using MultipartFormDataContent:

    HttpClient httpClient = new HttpClient();
    MultipartFormDataContent form = new MultipartFormDataContent();

    form.Add(new StringContent(token), "token");

    var imageForm = new ByteArrayContent(imagen, 0, imagen.Count());
    imagenForm.Headers.ContentType = new MediaTypeHeaderValue("image/jpg");

    form.Add(imagenForm, "image", "nameholder.jpg");

    HttpResponseMessage response = await httpClient.PostAsync("your_url_here", form);

    response.EnsureSuccessStatusCode();
    httpClient.Dispose();
    string result = response.Content.ReadAsStringAsync().Result;

You could refer these too:

There are plenty of samples out there, which I've not mentioned here. It would be great if you could give a search before you post here.

Community
  • 1
  • 1
Kulasangar
  • 7,225
  • 3
  • 36
  • 71
  • Thanks i've solved this problem , but now i need to resize the BitMapImage to send it to server , do you have any references about that ? – Yasser Hossam Feb 19 '15 at 15:04
  • You could go for the http://writeablebitmapex.codeplex.com/ & or the `WriteableBitmap` : http://stackoverflow.com/questions/9641574/how-do-i-resize-a-bitmapimage-to-50x50-on-windows-phone-7 – Kulasangar Feb 19 '15 at 15:10
  • i'm sorry it worked before i've tried your solution :) – Yasser Hossam Feb 20 '15 at 15:23