Request.Params.AllKeys [0]有时为null

时间:2016-01-11 09:00:42

标签: javascript c#

我有一个奇怪的问题,随机我的webmethod将返回Request.Params.AllKeys [0]的空值,如果我刷新页面并再次尝试它将工作。

基本上我正在做的是我有一个画布,我提交给这样的网络方法:

function convertCanvas(strType) {

            if (strType == "PNG") {
                if (ValidateForm('form1')) {
                    //var oImg = Canvas2Image.saveAsPNG(canvas, true);
                    //var canvasData = canvas.toDataURL("image/png");
                    var canvasData = document.getElementById("panel").toDataURL("image/png");
                    var xhr = new XMLHttpRequest();
                    xhr.open("POST", "/Job/SignatureSave", true);
                    xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                    xhr.setRequestHeader("Content-length", canvasData.length);
                    xhr.setRequestHeader("X-File-Name", "<%: Model.RequestMV.RequestID %>");

                    // subscribe to this event before you send your request.
                    xhr.onreadystatechange = function () {
                        if (xhr.readyState == 4) {
                            //alert the user that a response now exists in the responseText property.
                            if (xhr.responseText == 'yes') {
                                window.location.reload(true);
                                //OnSubmitForm();
                            }
                            else {
                                alert('Saving signature failed, please try again');
                            }
                            // And to view in firebug
                            console.log('xhr', xhr.responseText)
                            console.log('data:', canvasData)
                        }
                    }

                    xhr.send(canvasData);
                }
            }
        }

我知道canvasData确实包含数据,即使它警告显示签名没有保存,因为我在每个帖子的警报中都有canvasData,即使webmethod返回失败也总是存在数据。

我的网络方法是这样的:

[WebMethod]
    public string SignatureSave(string data)        
    {
        try
        {
            var b = Request.Params.AllKeys[0];
            var filename = Request.Headers["X-File-Name"];

            string imageAsBase64 = b.ToString();
            string fileName = filename.ToString();

            imageAsBase64 = imageAsBase64.Replace("data:image/png;base64,", "");
            imageAsBase64 = imageAsBase64.Replace(" ", "+");

            int remainder = imageAsBase64.Length % 4;

            for (int i = 4; i > remainder; i--)
            {
                imageAsBase64 = imageAsBase64 + "=";
            }

            string imagestring = imageAsBase64;


            byte[] imageAsBytes = Convert.FromBase64String(imagestring);

            var saveToFileLoc = Path.Combine(@"" + WebConfigurationManager.AppSettings["SignaturePath"].ToString(), fileName + ".png");

            // save the file.
            var fileStream = new FileStream(saveToFileLoc, FileMode.Create, FileAccess.ReadWrite);
            fileStream.Write(imageAsBytes, 0, imageAsBytes.Length);
            fileStream.Close();

            return "yes";

        }
        catch (Exception e)
        {
            return "no";
        }   


    }

为什么在javascript方面是那里的数据并在请求中发送,但在代码隐藏方面,它有时,并非所有时间,只是有时说params的第一个元素是null?

0 个答案:

没有答案