JavaScript:
var username = 'sami';
var myImage = canvas.toDataURL("image/png");
var ajax = new XMLHttpRequest();
ajax.open("POST", "Pic.php", false);
ajax.setRequestHeader('Content-Type', 'application/upload');
ajax.send(myImage);
ajax.send("q=" + encodeURIComponent(username));
PHP:
<? php
if (isset($GLOBALS["HTTP_RAW_POST_DATA"])) {
$user = $_POST['q'];
$comp = $user.".png";
$imageData = $GLOBALS['HTTP_RAW_POST_DATA'];
$filteredData = substr($imageData, strpos($imageData, ",") + 1);
$unencodedData = base64_decode($filteredData);
$fp = fopen($comp, 'wb');
fwrite($fp, $unencodedData);
fclose($fp);
} ?>
这是代码,问题是我无法让用户发布,当图像仅保存其“.png”时......