我目前使用 html2canvas 来获取 div 作为图像。
html2canvas([document.getElementById("board")],{
onrendered: function( canvas ) {
var img = canvas.toDataURL();
var request = $.ajax({
type:"POST",
url:"downloadPic.php",
data:{downloadPic : img}
});
然后,我想下载该图像(到浏览器默认下载目录)
if(isset($_POST['downloadPic'])){
$pic = $_POST['downloadPic'];
$filteredData=substr($pic, strpos($pic, ",")+1); //html2canvas adds this
$unencodeData=base64_decode($filteredData);
$filename = "downloadedPic";
header('Content-Description: File Transfer');
header('Content-Type: image/png');
header('Content-Disposition: attachment; filename="'.basename($filename).'"');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($unencodeData));
ob_clean();
flush();
readfile($unencodeData);
}
但这似乎不起作用。如果我执行 file_put_contents("downloadedPic", $unencodedData) 图片成功保存在我的服务器上。
我需要不同的标头配置吗?