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,")[];
}

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

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/hX3IjRILVdsESJn+FFAryP4zwY1iwuSu1pYCh99rf/ZV69EmPmbljX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n+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(, str.Length - );

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

Base-64字符串无效,The input is not a valid Base-64 string as it contains a non-base 64 character的更多相关文章

  1. winform错误提示 :窗口类名无效(Window class name is not valid)

    winfrom 程序在 xp 操作系统上报错提示 窗口类名无效(Window class name is not valid) 解决方法 注释 Program类 里 这句 Application.En ...

  2. git是一种分布式代码管理工具,git通过树的形式记录文件的更改历史,比如: base'<--base<--A<--A' ^ | --- B<--B' 小米工程师常常需要寻找两个分支最近的分割点,即base.假设git 树是多叉树,请实现一个算法,计算git树上任意两点的最近分割点。 (假设git树节点数为n,用邻接矩阵的形式表示git树:字符串数组matrix包含n个字符串,每个字符串由字符'0

    // ConsoleApplication10.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream& ...

  3. [Swift]LeetCode434. 字符串中的单词数 | Number of Segments in a String

    Count the number of segments in a string, where a segment is defined to be a contiguous sequence of ...

  4. [Swift]LeetCode806. 写字符串需要的行数 | Number of Lines To Write String

    We are to write the letters of a given string S, from left to right into lines. Each line has maximu ...

  5. [Swift]LeetCode833. 字符串中的查找与替换 | Find And Replace in String

    To some string S, we will perform some replacement operations that replace groups of letters with ne ...

  6. 循环遍历Java字符串字符的规范方法——类似python for ch in string

    比如我将string作为CNN 文本处理输入: float [] input = new float[maxLength]; // 1 sentence by maxLenWords // int[] ...

  7. C#LeetCode刷题之#557-反转字符串中的单词 III(Reverse Words in a String III)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3955 访问. 给定一个字符串,你需要反转字符串中每个单词的字符顺 ...

  8. C#LeetCode刷题之#345-反转字符串中的元音字母​​​​​​​(Reverse Vowels of a String)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3935 访问. 编写一个函数,以字符串作为输入,反转该字符串中的元 ...

  9. 字符串中判断存在的几种模式和效率(string.contains、string.IndexOf、Regex.Match)

    通常情况下,我们判断一个字符串中是否存在某值常常会用string.contains,其实判断一个字符串中存在某值的方法有很多种,最常用的就是前述所说的string.contains,相对来说比较常用的 ...

随机推荐

  1. css3弹性布局

    二.弹性布局(重点******************************************) 1.什么是弹性布局 弹性布局,是一种布局方式. 主要解决的是某个元素中子元素的布局方式 让页面 ...

  2. GreenPlum-数据存储目录迁移及常用操作

    一.环境介绍 Greenplum5 3节点集群,Centos7.2虚拟机, 二.需求 因为/home目录磁盘空间已满,需要将Greenplum的数据存储目录转移到新的分区/opt目录下,虚拟机磁盘管理 ...

  3. Java开发架构篇:领域驱动设计架构基于SpringCloud搭建微服务

    作者:小傅哥 博客:https://bugstack.cn 沉淀.分享.成长,让自己和他人都能有所收获! 一.前言介绍 微服务不是泥球小单体,而是具备更加清晰职责边界的完整一体的业务功能服务.领域驱动 ...

  4. 一篇文教你使用python Turtle库画出“精美碎花小清新风格树”快来拿代码!

    Turtle库手册可以查询查询 python图形绘制库turtle中文开发文档及示例大全,手册中现有示例,不需要自己动手就可以查看演示. 使用Turtle画树,看了一下网上的代码,基本上核心的方法是使 ...

  5. Pyqt5_QPushButton

    QPushButton 状态 isDown() 提示按钮是否已按下 isChecked() 提示按钮是否已经标记 isEnable() 提示按钮是否可以被用户点击 isCheckAble() 提示按钮 ...

  6. 一,初次接触html+css需要注意的小问题

    不足之处请不吝赐教,在评论区帮忙补充 html最基础的,入门学习的是标签,常用的标签有<a>      定义锚.<b>      定义粗体字.<br>      单 ...

  7. Redis学习笔记(1)

    一.NoSQL基础知识 1. NoSQL概念 NoSQL(NoSQL = Not Only SQL ),意即“不仅仅是SQL”,泛指非关系型的数据库.随着互联网web2.0网站的兴起,传统的关系数据库 ...

  8. 读Pyqt4教程,带你入门Pyqt4 _008

    QCalendarWidget QCalendarWidget 提供基于月份的日历窗口组件,它允许用户简单并且直观的选择日期. #!/usr/bin/python # -*- coding: utf- ...

  9. NodeJS——模块全局安装路径配置以及关于supervisor的问题解释

    下载安装NodeJS后,在自己选择的路径下会有如下的文件: 默认情况下NodeJS安装会同时安装npm(模块管理器:用于管理用户require的模块,有全局和本地两种). 注:全局:执行npm  in ...

  10. SpringBoot获取配置文件,就这么简单。

    在讲SpringBoot 获取配置文件之前我们需要对SpringBoot 的项目有一个整体的了解,如何创建SpringBoot 项目,项目结构等等知识点,我在这里就不一一讲述了,没有学过的小伙伴可以自 ...