简体   繁体   English

验证Base64字符串

[英]Validate Base64 string

I am converting an image to base64 on a html5 mobile client and posting the string to my webapi service. 我正在将图像转换为html5移动客户端上的base64并将字符串发布到我的webapi服务中。 I try to convert the received string back to an image and I get the following exception 我尝试将收到的字符串转换回图像,但出现以下异常

The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters. 输入内容不是有效的Base-64字符串,因为它包含非Base 64字符,两个以上的填充字符或填充字符中的非法字符。

Pastebin of base64 string is here . base64字符串的Pastebin在这里

I have read all the suggestions about replaing wrong characters so I wroe this function to and pass my strings into it first yet no luck. 我已经阅读了所有有关替换错误字符的建议,因此我将此函数编写为字符串,并先将其传递给它,但是还是没有运气。

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

Is there a way to know exactly what character is causing the conversion to fail? 是否有办法确切知道是什么字符导致转换失败?

My guess is that you're including the "data:image/png;base64," part at the start of the string - you need to strip that first. 我的猜测是,您要在字符串的开头包含"data:image/png;base64,"部分-您需要先去除该部分。 You don't need to do anything else - with the text in the pastebin, Convert.FromBase64String handles everything after the "base64," with no problems at all. 您无需执行其他任何操作-使用pastebin中的文本, Convert.FromBase64String处理“ base64”之后的所有内容,完全没有问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM