data post image and json same time

I'm trying to upload image and json in one request using c# code, but server always returns 400- bad request. Executing same request using fiddler returns status code 200. help...

Here is my fiddler code :

------WebKitFormBoundary7MA4YWxkTrZu0gW Content-Disposition: form-data; name="application/json" Content-Type: application/json

{"type": "Personal","comments": ["Lorem", "Ipsum" ] } ------WebKitFormBoundary7MA4YWxkTrZu0gW-- Content-Disposition: form-data; name="fieldNameHere"; filename="1111.jpg"

Content-Type: image/jpeg

<@INCLUDE C:UsersuserDesktopNew folder1111.jpg@>

And implementation in c#:

     var boundary = "Upload----" + DateTime.Now.Ticks.ToString();
                MultipartFormDataContent form = new MultipartFormDataContent(boundary);
                StringContent content = new StringContent(bodyJson);
                content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
                form.Add(content, "application/json");

                var imageContent = new ByteArrayContent(image);
                imageContent.Headers.ContentType = MediaTypeHeaderValue.Parse("image/jpeg");
                form.Add(imageContent, "image/jpeg", "image.jpg");
                var responseTask = _httpClient.PostAsync(url, form).Result;

the response is always same :

在这里输入图像描述


You can pass the parameter as a string content , check the below sample.

public async Task<JObject> ExecutePostAsync(Stream myStreem, string url, string token, string parameter1, string parameter2, string parameter3)
    {
        try
        {
            using (var content = new MultipartFormDataContent("----MyBoundary"))
            {

                using (var memoryStream = myStreem)
                {
                    using (var stream = new StreamContent(memoryStream))
                    {
                        content.Add(stream, "file", Guid.NewGuid().ToString() + ".jpg");
                        content.Add(new StringContent(parameter1), "parameter1");
                        content.Add(new StringContent(parameter3), "parameter2");
                        content.Add(new StringContent(parameter3), "parameter3");

                        using (HttpClient client = new HttpClient())
                        {
                            client.DefaultRequestHeaders.Add("Authorization", "Bearer " + token);
                            var responce = await client.PostAsync(url, content);
                            string contents = await responce.Content.ReadAsStringAsync();
                            return (JObject.Parse(contents));
                        }

                    }
                }
            }

        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

In API get the data from FORM request

    public async Task<IHttpActionResult> UploadFile()
    {

        string parameter1 = HttpContext.Current.Request.Form["parameter1"];
        string parameter2 = HttpContext.Current.Request.Form["parameter2"];
        string parameter3 = HttpContext.Current.Request.Form["parameter3"];

    }
链接地址: http://www.djcxy.com/p/75406.html

上一篇: 计算字符串中字符的出现次数

下一篇: 数据发布图像和json同一时间