Base-64 字符数组或字符串的长度无效等问题解决方案

  • Post author:
  • Post category:其他

项目特殊需要,调用ActiveX三维控件进行控件某一特殊部位的截图操作,这个截图保存由ActiveX控件控制保存到本地是没问题的,现在需要将这个截图上传到服务器,多人共享,就牵扯到需要读取本地文件……后沟通后,ActiveX控件方提供一个接口,返回相关截图文件的Base64编码字符串,由Web应用程序转换为对应格式,存储在服务器即可。

遇到问题:

Base-64 字符数组或字符串的长度无效

直接调用Convert.FromBase64String方法时,报“Base-64 字符数组或字符串的长度无效”的错误。

MemoryStream stream = new MemoryStream(Convert.FromBase64String(base64));

于是想到对ActiveX返回的Base64字符串进行编码操作

base64 = System.Web.HttpUtility.HtmlEncode(base64);
MemoryStream stream = new MemoryStream(Convert.FromBase64String(base64));

运行程序,报“输入的不是有效的 Base-64 字符串,因为它包含非 Base-64 字符、两个以上的填充字符,或者填充字符间包含非法字符”错误。

最终解决方案:

对Base64字符串进行特殊字符替换,并进行PadRight操作,解决问题

string dummyData = base64.Trim().Replace("%", "").Replace(",", "").Replace(" ", "+");
if (dummyData.Length % 4 > 0)
{
    dummyData = dummyData.PadRight(dummyData.Length + 4 - dummyData.Length % 4, '=');
}
MemoryStream stream = new MemoryStream(Convert.FromBase64String(dummyData));

最后提供一下封装的Base64Helper类,提供Base64字符串与文件之间相互转换操作。

/// <summary>
    /// Base64编码字符串与文件转换帮助泪
    /// </summary>
    public class Base64Helper
    {
        /// <summary>
        /// Base64编码字符串转文件
        /// </summary>
        /// <param name="base64">Base64编码字符串</param>
        /// <param name="filename">文件完整路径</param>
        public static void ConvertBase64ToFile(string base64, string filename)
        {
            //base64 = "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAIBAQEBAQIBAQECAgICAgQDAgICAgUEBAMEBgUGBgYFBgYGBwkIBgcJBwYGCAsICQoKCgoKBggLDAsKDAkKCgr/2wBDAQICAgICAgUDAwUKBwYHCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKC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Cs+JVfaF+cbEUWkeCfCiwIbkWGkaeFBd5rqYQxR9yd8sz7V6nc7H1JqG7sDmfiBb6w/jvRn8LTG31efRtRtLK/uNNlubW1Dy2UkjzKjKM7Yfk3MoLYGeTVG2h8P+H/ABYPCWjWttoXhDwZpZv9UxGbe0a5lZpEDtJb+U4jVZLh3ScOjvE8ifMrVctUuNJ0bXfjRrvhuGTVorK8fS4byE280Fgihkt2cvL5QlMKSvtC8su+MNHiuf8ADZk1/wCH3gb4U6f4is7ybVdFttV8RajoniOa+jltAqSSSQ3ZuobqSO4nYLHcbZA6b96gNioe4zpfhBb6jqtjffEvWWnW48T3C3NtbSXDMtrYqu22jCrdXEIYx/vGaEqrtIWKg5rsK5XUfiKmn/Gix+HFxdIqXejGaKEIAzzM0rKSxPICW02AvPUkYwR1pUHtVITIyoNN2HOKkKkUlVdgROn50xlB4NTkAio3Qn61SdwK7RjPNFS0UwJVAyBWL8UPE9/4I+GXiPxppMMMl1o+g3l9bR3CkxtJFC8ihgpBK5UZAIOO4ooqJASfD/RYtO0MavJe3V1eauEvL+5u5y7PI0aABRwsaKqgBECrwWwWZmbB+LD2+teNvB3w81iwiutL1m8vW1G2lZ9k6w2rkRSIG2SxtuO6ORWU4U4BUEFFR9lD6neIoPWvMf2Wb1NT8P64ws1t10nWF0OyghnlMUdpaW8SQqsbuyRnDsW2BQxOSKKKH8SHHY5D4jEj/goZ4AHr4Xn/APRWoV7+VB60UUR3ZQwjPBqM8HFFFUZhSOOM0UU1uBGVB5IoooqwP//";
            string dummyData = base64.Trim().Replace("%", "").Replace(",", "").Replace(" ", "+");
            if (dummyData.Length % 4 > 0)
            {
                dummyData = dummyData.PadRight(dummyData.Length + 4 - dummyData.Length % 4, '=');
            }
            MemoryStream stream = new MemoryStream(Convert.FromBase64String(dummyData));
            //filename = @"C:\Users\admin\Desktop\Tongji\test.jpg";
            Bitmap img = new Bitmap(stream);
            img.Save(filename);
        }

        /// <summary>
        /// 文件转Base64编码字符串
        /// </summary>
        /// <param name="fileName">文件完整路径</param>
        /// <returns></returns>
        public static string ConvertFileToBase64(string fileName)
        {
            //fileName = @"C:\Users\admin\Desktop\Tongji\test.jpg";
            FileStream fs = File.OpenRead(fileName);
            BinaryReader br = new BinaryReader(fs);
            byte[] bt = br.ReadBytes(Convert.ToInt32(fs.Length));
            string base64String = Convert.ToBase64String(bt);
            return base64String;
        }
    }

转载于:https://www.cnblogs.com/fonour/p/5857313.html