Base-64 字符数组或字符串的长度无效等问题解决方案
项目特殊需要,调用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 % > )
{
dummyData = dummyData.PadRight(dummyData.Length + - dummyData.Length % , '=');
}
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/2wBDAQICAgICAgUDAwUKBwYHCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgr/wAARCABkAGQDASIAAhEBAxEB/8Q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9yd8sz7V6nc7H1JqG7sDmfiBb6w/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 % > )
{
dummyData = dummyData.PadRight(dummyData.Length + - dummyData.Length % , '=');
}
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;
}
}
Base-64 字符数组或字符串的长度无效等问题解决方案的更多相关文章
- Base64 报错 的解决办法 (Base-64 字符数组或字符串的长度无效。, 输入的不是有效的 Base-64 字符串,因为它包含非 Base-64 字符、两个以上的填充字符,或者填充字符间包含非法字符。)
Base64 报错 的解决办法, 报错如下:1. FormatException: The input is not a valid Base-64 string as it contains a n ...
- C#编程中的Image/Bitmap与base64的转换及 Base-64 字符数组或字符串的长度无效问题 解决
最近用base64编码传图片遇到了点问题,总结下. 首先总结下base64编码的逻辑,来自网络:https://www.cnblogs.com/zhangchengye/p/5432276.html ...
- c语言字符数组与字符串的使用详解
转自:http://www.jb51.net/article/37456.htm 1.字符数组的定义与初始化字符数组的初始化,最容易理解的方式就是逐个字符赋给数组中各元素.char str[10]={ ...
- c语言中的字符数组与字符串
1.字符数组的定义与初始化 字符数组的初始化,最容易理解的方式就是逐个字符赋给数组中各元素. char str[10]={ 'I',' ','a','m',' ',‘h’,'a','p','p','y ...
- [日常] C语言中的字符数组和字符串
c语言字符数组和字符串:1.存放字符的数组称为字符数组 char str[]2.'\0'也被称为字符串结束标志3.由" "包围的字符串会自动在末尾添加'\0'4.逐个字符地给数组赋 ...
- C++基础之字符数组和字符串
无意中发现了一个非常有意思的技术类型小品文系列,通过大牛指导菜鸟的方式,解说讲C++知识,有的非常基础却是开发中easy忽略的地方. [Elminster的专栏] http://blog.csdn.n ...
- 灵魂拷问:Java如何获取数组和字符串的长度?length还是length()?
限时 1 秒钟给出答案,来来来,听我口令:"Java 如何获取数组和字符串的长度?length 还是 length()?" 在逛 programcreek 的时候,我发现了上面这个 ...
- Linux C\C++基础 字符数组、字符串和字符串常量
1.字符数组和字符串 C语言没有字符串类型,用字符数组模拟 字符串一定是字符数组,字符数组不一定是字符串 如果字符数组以字符'\0'('\0'等同与数字0)结尾,那么这个字符数组就是字符串 char ...
- C语言中整形数组、字符数组、字符串的区别
一. 第一 整型数组的存放,数组最后是不加'\0'的,字符串会自动加上,因此存放字符的时候数组的大小要比实际字符的多一个 第二 整型数组 每一个单元是4个字节的,字符串是一个一个字符存放的,每个字符占 ...
随机推荐
- OpenSceneGraph in ActiveX by ActiveQt
OpenSceneGraph in ActiveX by ActiveQt eryar@163.com Abstract. Qt’s ActiveX and COM support allows Qt ...
- ASP.NET Core应用针对静态文件请求的处理[5]: DefaultFilesMiddleware中间件如何显示默认页面
DefaultFilesMiddleware中间件的目的在于将目标目录下的默认文件作为响应内容.我们知道,如果直接请求的就是这个默认文件,那么前面介绍的StaticFileMiddleware中间件会 ...
- 从零开始编写自己的C#框架(27)——什么是开发框架
前言 做为一个程序员,在开发的过程中会发现,有框架同无框架,做起事来是完全不同的概念,关系到开发的效率.程序的健壮.性能.团队协作.后续功能维护.扩展......等方方面面的事情.很多朋友在学习搭建自 ...
- # PHP - 使用PHPMailer发邮件
PHPMailer支持多种邮件发送方式,使用起来非常简单 1.下载PHPMailer https://github.com/PHPMailer/PHPMailer,下载完成加压后, 把下边的两个文件复 ...
- dagger2系列之依赖方式dependencies、包含方式(从属方式)SubComponent
本篇是实战文章,从代码的角度分析这两种方式.本文参考自下列文章: http://www.jianshu.com/p/1d42d2e6f4a5 http://www.jianshu.com/p/94d4 ...
- System.Guid ToString五中格式
参考:https://msdn.microsoft.com/en-us/library/97af8hh4.aspx 测试代码: using System; using System.Collectio ...
- 代码的坏味道(17)——夸夸其谈未来性(Speculative Generality)
坏味道--夸夸其谈未来性(Speculative Generality) 特征 存在未被使用的类.函数.字段或参数. 问题原因 有时,代码仅仅为了支持未来的特性而产生,然而却一直未实现.结果,代码变得 ...
- Windows API 设置窗口下控件Enable属性
参考页面: http://www.yuanjiaocheng.net/webapi/create-crud-api-1-put.html http://www.yuanjiaocheng.net/we ...
- C#使用GET、POST请求获取结果
C#使用GET.POST请求获取结果,这里以一个简单的用户登陆为例. 1. 使用GET请求获取结果 1.1 创建LoginHandler.aspx处理页面 protected void Page_Lo ...
- .NET Portability Analyzer 已开源
在一年前介绍过<介绍.NET 开发必备工具 .NET Portability Analyzer>,微软已经把代码开源到Github:https://github.com/Microsoft ...