【问题标题】:Upload Image to a Chevereto Website C#将图像上传到 Chevereto 网站 C#
【发布时间】:2015-01-19 01:26:46
【问题描述】:

我的一个朋友正在尝试使用 chevereto API 将 Windows 窗体应用程序上的图像上传到 chevereto 网站并尝试取回链接(来自网站的响应),但它并没有真正起作用...

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;
        }
    }
}

谁能告诉我它是怎么做的?

提前致谢。

【问题讨论】:

  • 可以请你的朋友贴出不起作用的代码吗?
  • 已添加代码。感谢您的回复。

标签: c# post image-uploading


【解决方案1】:

没关系,伙计们,我的朋友已经解决了问题。

他使用WebRequest 发送和接收数据。 他还说,base64(+,=,/)的字符串不要忘记转义,否则api将无法正确接受,会返回无效的base64字符串。

还是谢谢。

【讨论】:

    猜你喜欢
    • 2019-03-22
    • 2013-07-28
    • 2013-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-14
    • 2020-09-03
    • 1970-01-01
    相关资源
    最近更新 更多