javascript ajax code:
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);
c# server side code in myPage.aspx - page load:
// 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);
The last server-side code is currently always raising an error.. Seems to be a base64 conversion error... "System.FormatException: 'Invalid length for a Base-64 char array or string.'" Any ideas? Thank you.
after test and trying, the problem was in javascript encoding... the right code is:
xhr.send("imgData=" + encodeURIComponent(dataURL));
Thank you all :-)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.