Uploading image and data as multi part content - windows phone 8

孤街醉人 提交于 2019-12-10 10:44:11

问题


I am unable to upload image and data to web service as multipart content.Here is my code

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

The response I get is 400- Bad request

Is is possible to send both image and data together like this? If yes, what is the proper way to go about?


回答1:


I can't test this right now, but the problem could be a missing boundary between your data items. To specify one, initializting your MultipartFormDataContent as follows:

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

More about boundaries: What is the boundary in multipart/form-data?



来源:https://stackoverflow.com/questions/23829071/uploading-image-and-data-as-multi-part-content-windows-phone-8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!