Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

A friend of my is trying to upload a image on a Windows Form App to a chevereto website, using chevereto API and trying to get the link back(response from website), but its not really working...

API: Chevereto API

UPDATE: Code Added

 static class Upload
{
    string apiKey = "DEFAULT_API_KEY";
    public string UploadImage(Image image)
    {
        WebClient webClient = new WebClient();

        webClient.Headers.Add("key", apiKey);
        webClient.Headers.Add("format", "txt");

        System.Collections.Specialized.NameValueCollection Keys =
            new System.Collections.Specialized.NameValueCollection();

        try
        {
            string ht = "http://";
            Keys.Add("image", ImageToBase64(image, ImageFormat.Bmp));
            byte[] responseArray = webClient.UploadValues(ht + "mysite.com/api/1/upload/", Keys);
            string result = Encoding.ASCII.GetString(responseArray);

            return result;
        }
        catch (Exception e)
        {
            InternalConsole.LogError("Cannot upload image, error on next line: ");
            InternalConsole.Log(e.Message);
            return "none";
        }
    }

    public string ImageToBase64(Image image, System.Drawing.Imaging.ImageFormat format)
    {
        using (MemoryStream ms = new MemoryStream())
        {
            // Convert Image to byte[]
            image.Save(ms, format);
            byte[] imageBytes = ms.ToArray();

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

Can someone show me how its done?

Thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
379 views
Welcome To Ask or Share your Answers For Others

1 Answer

Nevermind guys, my friend already fixed the problem.

He used WebRequest to send and receive the data. He also said, dont forget to escape the string of base64(+, =, /), or the api will not accept properly and will return invalid base64 string.

Thanks anyway.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...