【问题标题】:Byte image is not being saved in the database from client browser字节图像未从客户端浏览器保存在数据库中
【发布时间】:2014-12-07 13:39:16
【问题描述】:

我真的陷入了保存图像的问题。我正在开发一个网站(asp.net,VB),使用户能够将签名图像保存在数据库中。当我在 localhost 中运行时,我的代码有效(将图像保存在数据库中),但已发布的网站不保存。请帮我解决这个问题。

canvas class="roundCorners" id="txtSignature" style="position: relative; margin-left: auto; margin-right: auto; padding-left: 0; padding-right: 0; display: block; 边框: 1px 纯色#c4caac; 背景颜色:#fff" >

                                 <script type="text/javascript" >
                                     signatureCapture();
                                 </script>

画布的 JavaScript 是:

function signatureCapture() {
    var canvas = document.getElementById("txtSignature");
    var context = canvas.getContext("2d");
    canvas.width = 600;
    canvas.height = 200;
    context.fillStyle = "#fff";
    context.strokeStyle = "#444";
    context.lineWidth = 1.5;
    context.lineCap = "round";
    context.fillRect(0, 0, canvas.width, canvas.height);
    var disableSave = true;
    var pixels = [];
    var cpixels = [];
    var xyLast = {};
    var xyAddLast = {};
    var calculate = false;
    {   //functions
        function remove_event_listeners() {
            canvas.removeEventListener('mousemove', on_mousemove, false);
            canvas.removeEventListener('mouseup', on_mouseup, false);
            canvas.removeEventListener('touchmove', on_mousemove, false);
            canvas.removeEventListener('touchend', on_mouseup, false);

            document.body.removeEventListener('mouseup', on_mouseup, false);
            document.body.removeEventListener('touchend', on_mouseup, false);
        }

        function get_coords(e) {
            var x, y;

            if (e.changedTouches && e.changedTouches[0]) {
                var offsety = canvas.offsetTop || 0;
                var offsetx = canvas.offsetLeft || 0;

                x = e.changedTouches[0].pageX - (offsetx);
                y = e.changedTouches[0].pageY - (offsety);

            } else if (e.layerX || 0 == e.layerX) {
                x = e.layerX;
                y = e.layerY;
            } else if (e.offsetX || 0 == e.offsetX) {
                x = e.offsetX;
                y = e.offsetY;
            }

            return {
                x : x,
                y : y
            };
        };

        function on_mousedown(e) {
            e.preventDefault();
            e.stopPropagation();

            canvas.addEventListener('mouseup', on_mouseup, false);
            canvas.addEventListener('mousemove', on_mousemove, false);
            canvas.addEventListener('touchend', on_mouseup, false);
            canvas.addEventListener('touchmove', on_mousemove, false);
            document.body.addEventListener('mouseup', on_mouseup, false);
            document.body.addEventListener('touchend', on_mouseup, false);

            empty = false;
            var xy = get_coords(e);
            context.beginPath();
            pixels.push('moveStart');
            context.moveTo(xy.x, xy.y);
            pixels.push(xy.x, xy.y);
            xyLast = xy;
        };

        function on_mousemove(e, finish) {
            e.preventDefault();
            e.stopPropagation();

            var xy = get_coords(e);
            var xyAdd = {
                x : (xyLast.x + xy.x) / 2,
                y : (xyLast.y + xy.y) / 2
            };

            if (calculate) {
                var xLast = (xyAddLast.x + xyLast.x + xyAdd.x) / 3;
                var yLast = (xyAddLast.y + xyLast.y + xyAdd.y) / 3;
                pixels.push(xLast, yLast);
            } else {
                calculate = true;
            }

            context.quadraticCurveTo(xyLast.x, xyLast.y, xyAdd.x, xyAdd.y);
            pixels.push(xyAdd.x, xyAdd.y);
            context.stroke();
            context.beginPath();
            context.moveTo(xyAdd.x, xyAdd.y);
            xyAddLast = xyAdd;
            xyLast = xy;

        };

        function on_mouseup(e) {
            remove_event_listeners();
            disableSave = false;
            context.stroke();
            pixels.push('e');
            calculate = false;
        };
    }
    canvas.addEventListener('touchstart', on_mousedown, false);
    canvas.addEventListener('mousedown', on_mousedown, false);
}

function signatureSave() {

    var canvas = document.getElementById("txtSignature"); // save canvas image as data url (png format by default)
    var dataURL = canvas.toDataURL("image/png");
    document.getElementById("saveSignature").src = dataURL;

    dataURL = dataURL.replace(/^data:image\/(png|jpg);base64,/, "")
    //alert(dataURL);
    // Sending the image data to Server
    $.ajax({
        type: 'POST',
        url: 'Save_Picture.aspx/UploadPic',
        data: '{ "imageData" : "' + dataURL + '" }',
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        success: function (msg) {
            alert("Done, Picture Uploaded.");
        }
    });

    location.href = "Main.aspx";
};

【问题讨论】:

    标签: javascript asp.net vb.net image canvas


    【解决方案1】:

    您确定要这样做吗:

    $.ajax({
            //... removed to make this answer shorter 
            success: function (msg) {
                alert("Done, Picture Uploaded.");
            }
    });
    //will be executed even before ajax request is done
    location.href = "Main.aspx";
    

    不妨试试以下方法:

    $.ajax({
            //... removed to make this answer shorter 
            success: function (msg) {
                alert("Done, Picture Uploaded.");
                location.href = "Main.aspx";
            }
    });
    

    【讨论】:

    • 谢谢!这救了我的命。非常感谢。
    猜你喜欢
    • 2019-09-10
    • 1970-01-01
    • 1970-01-01
    • 2013-04-10
    • 2014-03-14
    • 2013-07-18
    • 1970-01-01
    • 1970-01-01
    • 2017-06-04
    相关资源
    最近更新 更多