George_Fal 2013-03-06 19:53 采纳率: 0%
浏览 82

将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条回答

  • weixin_33696822 2013-03-06 20:06
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题