简体   繁体   中英

How to upload image with base64 format?

I am trying to do image upload with base64 format using httpclient, anybody has experienced on this?

            using (var client = new HttpClient())
            {
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                var detect = new Dictionary<string, string>
                {
                   { "api_key", API_KEY },
                   { "api_secret", API_SECRET },
                   { "urls", _base64 }
                };

                var content = new FormUrlEncodedContent(detect);
                var response = await client.PostAsync(DETECT_URL, content);

                HttpStatusCode _statusCode = response.StatusCode;

            }

the above codes returns BadRequest, please help

many thanks in advance

Don

a,
FormUrlEncodedContent is application/x-www-form-urlencoded not application/json .

b,
If you have the image loaded you can convert it to base64 using this:

    public string ImageToBase64(Image image)
    {
        using (MemoryStream m = new MemoryStream())
        {
            image.Save(m, image.RawFormat);
            byte[] imageBytes = m.ToArray();

            // Convert byte[] to Base64 String
            string base64String = Convert.ToBase64String(imageBytes);
            return base64String;
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM