【问题标题】:Validate Base64 string验证 Base64 字符串
【发布时间】:2014-02-01 09:08:36
【问题描述】:

我在 html5 移动客户端上将图像转换为 base64 并将字符串发布到我的 webapi 服务。我尝试将接收到的字符串转换回图像,但出现以下异常

输入不是有效的 Base-64 字符串,因为它包含非 base 64 字符、两个以上的填充字符或非法字符 在填充字符之间。

Pastebin of base64 string is here.

我已阅读有关替换错误字符的所有建议,因此我将这个函数写入并首先将我的字符串传递给它,但没有运气。

    private string FixBase64ForImage(string Image)
    {
        System.Text.StringBuilder sbText = new System.Text.StringBuilder(Image, Image.Length);

        sbText.Replace("\r\n", String.Empty);

        sbText.Replace(" ", String.Empty);

        sbText.Replace('-', '+');

        sbText.Replace('_', '/');

        sbText.Replace(@"\/", "/");

        return sbText.ToString();
    }

有没有办法确切知道是什么字符导致转换失败?

【问题讨论】:

    标签: c# html base64


    【解决方案1】:

    我的猜测是你在字符串的开头包含了"data:image/png;base64," 部分——你需要先去掉它。您无需执行任何其他操作 - 使用 pastebin 中的文本,Convert.FromBase64String 可以处理“base64”之后的所有内容,完全没有问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-22
      • 1970-01-01
      • 2021-01-08
      • 2011-02-21
      • 1970-01-01
      相关资源
      最近更新 更多