Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/469.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaScriptAjax将canvas.toDataURL(“image/jpeg”)发布到asp.net web表单_Javascript_C#_Asp.net_Ajax - Fatal编程技术网

JavaScriptAjax将canvas.toDataURL(“image/jpeg”)发布到asp.net web表单

JavaScriptAjax将canvas.toDataURL(“image/jpeg”)发布到asp.net web表单,javascript,c#,asp.net,ajax,Javascript,C#,Asp.net,Ajax,javascript ajax代码: 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 () {

javascript ajax代码:

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);
myPage.aspx中的c#服务器端代码-页面加载:

// 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);
//enctype=“多部分/表单数据”
字符串img=Server.UrlDecode(Request.Form[“imgData”]);
img=Regex.Match(img,@“数据:图像/(?。+),(?。+)”)。组[“数据”]。值;
//img=img.Replace(“数据:image/png;base64,”,”);
img=img.Trim('\0');
//byte[]bytes=Encoding.UTF8.GetBytes(img);
byte[]bytes=Convert.FromBase64String(img);
最后一个服务器端代码当前总是引发错误。。 似乎是base64转换错误。。。
“System.FormatException:'Base-64字符数组或字符串的长度无效。“” 有什么想法吗?
谢谢。

经过测试和尝试,问题在于javascript编码。。。 正确的代码是:

xhr.send("imgData=" + encodeURIComponent(dataURL));

谢谢大家:-)

出现了什么错误?System.FormatException:“Base-64字符数组或字符串的长度无效。谢谢。您从请求中得到了什么。表单[“imgData”]?