用javascript或jquery替换php脚本来解码base64

时间:2012-12-30 19:23:28

标签: javascript jquery html

我使用以下代码将我的画布作为png保存到服务器: 现在,我想用js或jquery替换php,但我不知道用base64解码做这个。

文件夹:imgs

HTML:  链接id =“save”以使用download-link打开缩略图

使用Javascript:

    $("#save").click(function () {
        $("#result").html("<img src=" + d.toDataURL() + ' /><br /><a href="#" id="get" class="minimal" >Download This</a>');
        $("#data").val(d.toDataURL());
        $("#get").click(function () {
            $("#frm").trigger("submit")
        })
    });
    $("#clear").click(function () {
        e.fillStyle = "#fff";
        e.fillRect(0, 0, d.width, d.height);
        e.strokeStyle = "red";
        e.fillStyle = "red"
    })
})

function saveRestorePoint() {
    var oCanvas = document.getElementById("can");
    var imgSrc = oCanvas.toDataURL("image/png");
    restorePoints.push(imgSrc);
}

PHP:

<?php
if (isset($_POST['data'])) {

    $data = $_POST['data'];

    //removing the "data:image/png;base64," part
    $uri = substr($data, strpos($data, ",") + 1);

    $filenamex = time();

    $filename = 'imgs/' . $filenamex . '.png';

    // put the data to a file
    file_put_contents($filename, base64_decode($uri));
    ///*
    //force user to download the image
    if (file_exists($filename)) {
            // We'll be outputting a PNG
            header('Content-type: image/png');
            // It will be called wow.png
            header('Content-Disposition: attachment; filename="' . $filename . '"');
            // The PDF source is in wow.png
            readfile($filename);
            exit();
    }
}
?>

0 个答案:

没有答案