上传图像和数据作为多部分内容- windows phone 8

本文关键字:windows phone 多部 图像 数据 | 更新日期: 2023-09-27 18:05:01

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

 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-错误请求

有可能像这样同时发送图像和数据吗?如果是,正确的做法是什么?

上传图像和数据作为多部分内容- windows phone 8

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

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

关于边界的更多内容:multipart/form-data中的边界是什么?