使用httpclient类windows phone 8将照片发送到服务器

时间:2015-02-19 14:24:42

标签: windows-phone-8 httpclient

我试图使用httpclient类将照片发送到服务器,但每次我尝试获取0字节文件时,这里是我发送图像的代码

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");
                            }
                        });
                     }

我还检查了e.ChoosenPhoto的长度,它不是0

1 个答案:

答案 0 :(得分:0)

使用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;

您也可以参考这些:

那里有很多样品,我在这里没有提到。如果你能在发布之前进行搜索,那就太棒了。