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

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

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

最佳答案

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

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

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

关于c# - 将图像和数据作为多部分内容上传 - Windows Phone 8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23829071/

相关文章:

c# - 如何使用反射获取泛型类的名称?

C# - 是否可以使用新接口(interface)扩展现有的内置类

c# - 将 OPC UA 服务器添加到 .NET 应用程序

c# - 我可以限制 ThreadPool 中线程的创建吗?

xaml - 即使已明确设置,Windows Phone 列表框项目背景也会改变颜色

C# 隐式转换运算符和 is/as 运算符

c# - 为什么无法在即时窗口中评估 lambda?

c# - 您如何在特定操作中重定向到 anchor

c# - WPF 中的自动数据更新

windows-phone-7 - 如何从 Windows Phone 7 或 8 获取用户自己的手机号码