Here is the JavaScript AJAX code snippet:
var dataURL = canvas.toDataURL("image/jpeg");
var xhr = new XMLHttpRequest();
xhr.open("POST", "myPage.aspx", true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.onreadystatechange = function () {
if (xhr.readyState != 4) return;
if (xhr.status != 200) {
alert("Status: " + xhr.status);
} else {
alert(xhr.responseText);
}
};
xhr.send("imgData=" + dataURL);
This is the C# server-side code within myPage.aspx's page load event:
// enctype="multipart/form-data"
string img = Server.UrlDecode(Request.Form["imgData"]);
img = Regex.Match(img, @"data:image/(?<type>.+?),(?<data>.+)").Groups["data"].Value;
//img = img.Replace("data:image/png;base64,", "");
img = img.Trim('\0');
//byte[] bytes = Encoding.UTF8.GetBytes(img);
byte[] bytes = Convert.FromBase64String(img);
An error is consistently being raised by the last block of server-side code... It appears to be related to a base64 conversion issue... "System.FormatException: 'Invalid length for a Base-64 char array or string.'" Any suggestions on how to resolve this? Thank you.