Base64 报错 的解决办法 (Base-64 字符数组或字符串的长度无效。, 输入的不是有效的 Base-64 字符串,因为它包含非 Base-64 字符、两个以上的填充字符,或者填充字符间包含非法字符。)
Base64 报错 的解决办法, 报错如下:
1. FormatException: The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or a non-white space character among the padding characters. ]
System.Convert.FromBase64String(String s) +0
System.Web.UI.ObjectStateFormatter.Deserialize(String inputString) +77
System.Web.UI.ObjectStateFormatter.System.Web.UI.IStateFormatter.Deserialize(String serializedState) +4
2. 输入的不是有效的 Base-64 字符串,因为它包含非 Base-64 字符、两个以上的填充字符,或者填充字符间包含非法字符。
3. Base-64 字符数组或字符串的长度无效。
解决方案:过滤特殊字符即可
using System; using Microsoft.VisualStudio.TestTools.UnitTesting; using MnCnyInterface; using System.Web; using System.Collections.Generic; using System.Collections.Specialized; using System.Linq; using System.IO; namespace UnitTestProject1 { [TestClass] public class UnitTest1 { [TestMethod] public void TestMethod1() { try { 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/8dzWNLFJC5SVGjcdVYYNbRnGXwu5x1KFSlpUi16qwyiiiqMgop8cbyuEjRnY9AoyafdW09pJ5d1BLDJgNtkQqcHocGldbD5Xa9tCGiiimIKKKKACiiigAooooAKKKKACiiigAooooAKKKKACvePCOh6PFoem3P8AZdsZpbeORpHTeSxUEnJzjn0rwevorw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No+YPijHPt93/BPW5fipZCQtFp1y+OgZ1X/GqF18V7lgfsulQxn1kmL/yArzOitFgKC+z+ZhPiLMJ/8vLeiX+R1Op+O9dvoniFwtrE/wB5bddpP/AuT+tcueTzSUV0wpwpq0FY8uvia2JlzVpOT8woooqzAKKKKACiiigAooooAKKKKAP/2Q3D3D"; //过滤特殊字符即可 string dummyData = imgData.Trim().Replace("%", "").Replace(",", "").Replace(" ", "+"); if (dummyData.Length % 4 > 0) { dummyData = dummyData.PadRight(dummyData.Length + 4 - dummyData.Length % 4, '='); } byte[] byteArray = Convert.FromBase64String(dummyData); } catch (System.Exception exp) { // Error creating stream or reading from it. System.Console.WriteLine("{0}", exp.Message); return; } } } }
好消息,
程序员专用早餐机。和掌柜说 ideaam,可以节省20元。 点击链接 或復·制这段描述¥k3MbbVKccMU¥后到淘♂寳♀
或者 淘宝扫码 支持下同行哈
----------------------------------------------------------------------------------------------------------------------------