字符串长度是4的倍数,所以我不明白为什么会收到此错误。
private static void SaveImageToFile(string imageString)
{
try
{
string base64Data = Regex.Replace(imageString, @"\s+", "");
base64Data = System.Web.HttpUtility.UrlDecode(base64Data.Replace('-', '+').Replace(" ","").Replace('_', '/'));
if (base64Data.Length % 4 > 0)
base64Data = base64Data.PadRight(base64Data.Length + 4 - base64Data.Length % 4, '=');
var length = base64Data.Length;
var binData = Convert.FromBase64String(base64Data);
}
catch (Exception ex) { }
}
“ base-64字符数组或字符串的长度无效” 错误被抛出FromFrom6464String
这是我传递的字符串: iVBORw0KGgoAAAANSUhEUgAABwgAAAAPCAYAAADwMZuwAAAFW0lEQVR4Xu3d22scZRzG8efd8 + wpu9lNNocebJsEEUsDXnggKI1tU42CeiF4qFYERamlYgsW + J + otQheeeN%2f4IXeeRmoemNPoDWpxXVrc9pkN3uamR3ZSf0jxvkODC9z93s + u3cP7%2fuaVq%2fjbXd2tNas65%2fGxu66va7l1b%2fV7rU1li%2frvadf1sTQiHgQQAABBBBAAAEEEEAAAQQQQAABBBBAAAEEEEAAAQQQQCDYAubbq997rV5Hg5Kw0Wmp2W3560ZzS27f1Vi + pFcfO67RfFEyxk%2fbsbvqOraePHA42OmZHgEEEEAAAQQQQAABBBBAAAEEEEAAAQQQQAABBBBAAIGQCZiXvr7g2Y7tF35uvy + n78p1d99YNKpyZkhzU7Oq5EuKxWJq210NCsWu09Mnz74RMi7iIoAAAggggAACCCCAAAIIIIAAAggggAACCCCAAAIIIBBsAbP41TnPGRSDruMXhINi0BkUhH1XURORFU + qkilqemy%2fRgvDulFbUTZlqZIbpiAM9m%2fP9AgggAACCCCAAAIIIIAAAggggAACCCCAAAIIIIAAAiEUMCevnPWSsYTikah2em2lYknFIzFV6%2fdlu46saEKV7LAeHntIlUJJ1 + 8tK2el%2faNHP55%2fPYRkREYAAQQQQAABBBBAAAEEEEAAAQQQQAABBBBAAAEEEEAguAJm4cpZbyiVUT6V0eCGwYKVU0RGV%2F + 8qWanpUw8panS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AIIIIAAAgj8zwT + BRNcWuMhdT3NAAAAAElFTkSuQmCC
function DownloadAsImage() {
var element = $("#map-container")[0];
html2canvas(element).then(function (canvas) {
var myImage = canvas.toDataURL().replace('data:image/png;base64,',
'');
var url = '/umbraco/api/countymap/SaveMapImage';
$.ajax({
url: url,
data: myImage,
dataType: 'json',
type: 'POST',
processData: false,
success: function (data) {
console.log('It worked!');
}
});
//downloadURI(myImage, "map.png");
});
}