php如何打开另存为对话框?脚本下载自动

时间:2014-08-01 22:49:26

标签: javascript php download html2canvas

我有一个下载按钮,可以激活htmlcanvas并下载html并将其保存为png文件。我没有配置它,因为我不懂javascript。我已经玩了几个下载文件的教程,但我无法在点击时打开一个对话框,无论我尝试将图像下载到该页面正在查看的任何设备的下载文件夹中。我认为这是javascript而不是php的问题?

<script type="text/javascript">
var $loading = $('#loader').hide();
$('#carddownload').on('click', function() {
showimage(); 
});
$(document)
.ajaxStart(function () {
$loading.show();
})
.ajaxStop(function () {
$loading.hide();
});
function showimage(){
html2canvas([document.getElementById('card')], {
onrendered: function(canvas)
{
var img = canvas.toDataURL()
$.post("save.php", {data: img}, function (file) {
window.location.href ="downloadpng.php?path="+ file}); 
}
});
}
</script>

<?php
$file = trim($_GET['path']);
// force user to download the image
if (file_exists($file)) {
    header('Content-Type: application/download');
    header('Content-Description: File Transfer');
    header('Content-Type: image/png');
    header('Content-Disposition: attachment; filename='.basename($file));
    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($file));
    ob_clean();
    flush();
    readfile($file);
    unlink($file);
    exit;
}
else {
    echo "error not found";
}

?>

0 个答案:

没有答案