将图像上传到Chevereto网站C# [英] Upload Image to a Chevereto Website C#

查看:75
本文介绍了将图像上传到Chevereto网站C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的一个朋友正在尝试使用chevereto API将Windows Form App上的图像上传到chevereto网站,并尝试重新获得链接(来自网站的响应),但是它确实无法正常工作...

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

 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?

谢谢.

推荐答案

没关系,我的朋友已经解决了这个问题.

Nevermind guys, my friend already fixed the problem.

他使用 WebRequest 来发送和接收数据. 他还说,别忘了转义base64(+,=,/)的字符串,否则api将无法正确接受并且将返回无效的base64字符串.

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.

还是谢谢.

这篇关于将图像上传到Chevereto网站C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆