将图像和数据上传为多部分内容 - Windows Phone 8

时间:2014-05-23 12:19:45

标签: c# .net windows-phone-8 upload multipartform-data

我无法将图片和数据作为多部分内容上传到网络服务。这是我的代码

 var fileUploadUrl = @"http://myurl";
                    var client = new HttpClient();
                    client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "multipart/form-data");
                    photoStream.Position = 0;

                    // This is the postdata
                    MultipartFormDataContent content = new MultipartFormDataContent();
                    content.Add(new StreamContent(photoStream), "attendeedImage");
                    content.Add(new StringContent("12", Encoding.UTF8), "userId");
                    content.Add(new StringContent("78", Encoding.UTF8), "noOfAttendees");
                    content.Add(new StringContent("chennai", Encoding.UTF8), "locationName");
                    content.Add(new StringContent("32.56", Encoding.UTF8), "longitude");
                    content.Add(new StringContent("32.56", Encoding.UTF8), "latitude");

                    Console.Write(content);
                    // upload the file sending the form info and ensure a result.
                    // it will throw an exception if the service doesn't return a valid successful status code
                    await client.PostAsync(fileUploadUrl, content)
                        .ContinueWith((postTask) =>
                        {
                            postTask.Result.EnsureSuccessStatusCode();
                        });

我收到的回复是400-错误请求

是否可以像这样一起发送图像和数据? 如果是,那么正确的方法是什么?

1 个答案:

答案 0 :(得分:1)

我现在无法对此进行测试,但问题可能是您的数据项之间缺少边界。要指定一个,请按以下步骤初始化MultipartFormDataContent

string boundary = "---###---"; // should never occur in your data
MultipartFormDataContent content = new MultipartFormDataContent(boundary);

有关边界的更多信息:What is the boundary in multipart/form-data?