Base-64字符串无效,The input is not a valid Base-64 string as it contains a non-base 64 character

base64规则:

* 字符串只可能包含A-Z,a-z,0-9,+,/,=字符
* 字符串长度是4的倍数
* =只会出现在字符串最后,可能没有或者一个等号或者两个等号

 

首先,C# 做上传文件的时候,需要替换掉这些字符:

data:image/jpeg;base64,

data:image/png;base64,

data:image/gif;base64,

if (str.Contains("base64,"))
{
  str = str.Split("base64,")[1];
}

如果还存在错误的话,可以进行一些字符替换试试:

var imgData = "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAYEBQYFBAYGBQYHBwYIChAKCgkJChQODwwQFxQYGBcUFhYaHSUfGhsjHBYWICwgIyYnKSopGR8tMC0oMCUoKSj/2wBDAQcHBwoIChMKChMoGhYaKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCj/wAARCADwAPADASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwD5UooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACip7azubpsWtvNMfSNC38q2LXwf4guTiLSrgH/poAn/oRFRKpCPxOxtTw1ar/AA4N+ibMCiuwi+HPiJhmS3hiH+3Mv9M1Yi+GeuyDO+yUe8jf/E1k8VRX2kdccoxstqUvuOHoru2+F+ugcS2J+krf/E1TuPh54hhHFtDJ/uTL/XFCxVF/aQSyjGx3pS+45City78J69aLmbSror6xp5g/8dzWNLFJC5SVGjcdVYYNbRnGXwu5x1KFSlpUi16qwyiiiqMgop8cbyuEjRnY9AoyafdW09pJ5d1BLDJgNtkQqcHocGldbD5Xa9tCGiiimIKKKKACiiigAooooAKKKKACiiigAooooAKKKKACvePCOh6PFoem3P8AZdsZpbeORpHTeSxUEnJzjn0rwevorwTIsvhLSeQcWyKR9BivMzOTjBWfU+q4VpwnXnzxT0/U2CkflYVVCAcADGKS2XEfuabIDECV+6eo9KktzmIYrwz7/oMb95Lt/hXrUjuI15/AUyH77/WibiVGb7tAB5xHLIQKkBV145BoBDDgg1GBsmAHRu1IBE/dybD909KbeW8VxFiaNJAOcOoIp9wMKG9Dmpeop36iavuZEuiaO6Bn0nT3Y+tsh/pTrfw9pEXI0uwDeot0GP0q5bgs5z0Sp5H2IT+VVzy2uZfV6V/hX3IjRILVd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tcfebipI0CL6nuaAIvILkGVifavG/jDcpJ4litoulvAA3szEn+W2vX9T1CLT7Oe5mOI4ULufQCvnDVr6XU9Tub2f/WTyFyPTPQfgOK9PLKblNzeyPlOKsUoYeOHW8nf5L/glSiiivcPgQooooAKKKKACiiigAooooAKKKKACiiigAooooAKtaZfT6ZqEF5aPsnhbcp/ofY9Kq0Umk1ZlRk4NSi7NH0V4e1m313SUv7LgkbZos8o3cH/AD0rYt23RAivnfwr4huvDupC5t/nib5ZoSeJF/x9DXunhzWLTV7QXFjJujb+E/eQ/wB0jsa+exmEdB3Xwn6XkucRzCnyT0qLdd/Nf1oacPEkgPXNEuVdXxkDrRICrh1H1FSI6sOCDXEe4Ijq/SmH5Zxj+LipCVUc4FRJ+8k3Y+UdKQDrgfJn0Oak7VFOchV9TUvagCtbDLsT0XgVNM+1eOp4FMtsHfj1rhfiJ41j0xXsNKkV78jDuvIh/wDsvbtWtKlKtPlicuMxlLB03VrOyX4+SML4reI1lf8AsSyfKRsGunB+8w6J+HU++PSvNqVmLMWYksTkk9TSV9NQoqjBQR+WY/Gzx1d1p9dvJdgooorU4wooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACtLQtavdDvRc2Eu1ujIeVcehFZtFKUVJWexdOpOlJTg7NdT2PTfilpkkCjULW6hmx83lgOv55B/SrbfETw43O+6B9oTXiNFcLy2i3dXPfhxPjoqzafy/yPbT8RfDi87rt/8Atl/9emSfFHQ0GEt79/pGo/m1eK0Uv7No+YPijHPt93/BPW5fipZCQtFp1y+OgZ1X/GqF18V7lgfsulQxn1kmL/yArzOitFgKC+z+ZhPiLMJ/8vLeiX+R1Op+O9dvoniFwtrE/wB5bddpP/AuT+tcueTzSUV0wpwpq0FY8uvia2JlzVpOT8woooqzAKKKKACiiigAooooAKKKKAP/2Q3D3D";
//过滤特殊字符即可    
string dummyData = imgData.Trim().Replace("%", "").Replace(",", "").Replace(" ", "+");byte[] byteArray = Convert.FromBase64String(dummyData);

上面两步操作完了还没有解决问题的话,可以调试看看base64字符串的长度,如果长度不是4的倍数,比如8057或者8041,可以尝试截掉最后一个字符,使字符长度变为4的倍数。(能正常解决bug,但不确定是否会产生其他问题)

str = str.Substring(0, str.Length - 1);

上面两个长度其实是本人调试的时候遇到的,神奇的是,复制粘贴后就会变成8056长度,可以正常解析。如果有大佬知道是怎么回事的话,欢迎评论沟通。

posted @ 2020-06-11 15:03  magel  阅读(6124)  评论(0编辑  收藏  举报