【问题标题】:Sending a photo to server using httpclient class windows phone 8使用 httpclient 类 windows phone 8 将照片发送到服务器
【发布时间】:2015-04-20 22:36:35
【问题描述】:

我正在尝试使用 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

【问题讨论】:

    标签: windows-phone-8 httpclient


    【解决方案1】:

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

    你也可以参考这些:

    那里有很多样本,我在这里没有提到。如果您可以在此处发布之前进行搜索,那就太好了。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多