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

  • 2020 年 6 月 11 日
  • 筆記

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/pTrfw9pEXI0uwDeot0GP0q5bgs5z0S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Ku6MjuOlADfMkHVKaZ3LbVTmlMn7tcfebipI0CL6nuaAIvILkGVifavG/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長度,可以正常解析。如果有大佬知道是怎麼回事的話,歡迎評論溝通。