将Canvas映像保存到mysql

Every 5 seconds I have my javascript running to call the update.php file (which will hopefully add the base64 code to the mysql - although yet to figure out).

Currently, I just have the base64 code writing out to a text area called debugConsole.

I just don't get how I'm supposed to pass the javascript variable to my php update file.

HTML:

<script type="text/javascript">
function saveViaAJAX()
{
    var testCanvas = document.getElementById("testCanvas");
    var canvasData = testCanvas.toDataURL("image/png");
    var postData = "canvasData="+canvasData;
    var debugConsole= document.getElementById("debugConsole");
    debugConsole.value=canvasData;

    //alert("canvasData ="+canvasData );
    var ajax = new XMLHttpRequest();
    //ajax.open("POST",'testSave.php',true);
    ajax.setRequestHeader('Content-Type', 'canvas/upload');
    //ajax.setRequestHeader('Content-TypeLength', postData.length);


    ajax.onreadystatechange=function()
    {
        if (ajax.readyState == 4)
        {
            //alert(ajax.responseText);
            // Write out the filename.
                document.getElementById("debugFilenameConsole").innerHTML="Saved as<br><a target='_blank' href='"+ajax.responseText+"'>"+ajax.responseText+"</a><br>Reload this page to generate new image or click the filename to open the image file.";
        }
    }

    ajax.send(postData);
}
setInterval(function() { saveViaAJAX(); }, 5000);
</script>

<script language='javascript' type='text/javascript'>
//Every 5 seconds calls php update
//syncronized jax:
function myjax() {
    oXhr = new XMLHttpRequest();
    oXhr.open("POST", "update.php?game=<?php echo $game; ?>", false);
    oXhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=utf-8");
    oXhr.send(null);
}

//set an interval each 5 seconds to call your myjax method
setInterval(function() { myjax(); }, 5000);
</script>
</head>



     <div>
        <canvas id="testCanvas" width="300" height="300"></canvas>
      </div>
      <div>
    <textarea id="debugConsole" rows="10" cols="60">Data</textarea>


      </div>
    <script type="text/javascript">
        // This portion of the code simply draws random circles into the canvas (it has nothing todo with saving the canvas).
        var canvas = document.getElementById("testCanvas");
        if (canvas.getContext)
        {
            var canvasContext = canvas.getContext("2d");
            for (i=0; i<150; i++)
            {
                canvasContext.fillStyle="rgb("+(parseInt(Math.random()*255))+","+(parseInt(Math.random()*255))+","+(parseInt(Math.random()*255))+")";
                canvasContext.beginPath();
                canvasContext.arc(Math.random()*350,Math.random()*350,Math.random()*20, 0,Math.PI*2,true);
                canvasContext.fill();
            }
        }
    </script>

PHP:

<?php

$random =  rand(); 
$game = $_GET["game"];

mysql_query("UPDATE paint_s SET paint_points='$random' WHERE id ='$game'") or die(mysql_error());
echo "Updated";
?>

Sorry for my lack of knowledge, I have been doing a few tutorials and was just trying to expand on the tutorial.

1个回答


您要做的第一件事就是打开您的请求</ p>

  ajax.open(“ POST”,'testSave.php',true);
</ code> </ pre>

然后要以 application / x-www-url-form-encoded </ code>发送数据,因此将其设置为内容类型</ p>

  ajax.setRequestHeader('Content-Type','application / x-www-url-form-encoded');
</ code> </ pre>

要确保数据正确编码,请使用 encodeURIComponent </ code> </ p>

  var postData =“ canvasData =” + encodeURIComponent(canvasData);
</ code> </ pre>

您还应该检查状态以查看请求是否成功</ p>

  if(ajax.readyState == 4)
{
     如果(ajax.status == 200){
     //成功
     }
     其他{
     //失败
     }
}
</ code> </ pre>

我不知道每5秒保存一次base64编码的图像是否是个好主意。</ p>
     </ div>

展开原文

原文

First thing you want to do is open your request

ajax.open("POST",'testSave.php',true);

then want to send your data as application/x-www-url-form-encoded so set it in the content type

ajax.setRequestHeader('Content-Type', 'application/x-www-url-form-encoded');

To make sure your data is properly encoded use encodeURIComponent

var postData = "canvasData="+encodeURIComponent(canvasData);

You should also check the status to see if the request actually suceeded

if (ajax.readyState == 4)
{
    if (ajax.status == 200){
    //success
    }
    else{
    //failure
    }
}

I don't know if saving a base64 encoded image every 5 seconds is a good idea though.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐