Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
385 views
in Technique[技术] by (71.8m points)

post - Upload Image to a Chevereto Website C#

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

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

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...