从jQuery Noty Button初始化php下载

时间:2013-03-21 18:11:12

标签: php jquery download

当点击jquery noty中的按钮时,我正在尝试使用PHP启动下载。当应该出现noty并且在firebug控制台中没有错误时,我得到空白页面。

var noty_id = noty({
text: 'blah',
type: 'success',
buttons: [
    {addClass: 'btn btn-primary', text: 'Download', onClick: function($noty) {

        $noty.close();
        <?php

        if (file_exists($file)) {
            header('Content-Description: File Transfer');
            header('Content-Type: application/octet-stream');
            header('Content-Disposition: attachment; filename='.basename($file));
            header('Content-Transfer-Encoding: binary');
            header('Expires: 0');
            header('Cache-Control: must-revalidate');
            header('Pragma: public');
            header('Content-Length: ' . filesize($file));
            ob_clean();
            flush();
            readfile($file);
            exit;
        }
        ?>  
    }}
]
});

1 个答案:

答案 0 :(得分:0)

没有ajax就可以工作了。我从jquery.noty.js

更改了第64行

自:

var $buttons = $('<div/>').addClass('noty_buttons');

要:

var $buttons = $("<form method='post'><input type='hidden' name='download' id='download'</form>").addClass('noty_buttons');

然后我在noty调用期间将值赋给隐藏字段:

var noty_id = noty({
    text: 'blah',
    type: 'success',
    buttons: [
        {addClass: 'btn btn-primary', text: 'Download', onClick: function($noty) {

            document.getElementById('download').value = file;
            $noty.close();  
        }}
   ]
 });

之后剩下的就是在发布表单后启动下载

<?php    
if(isset($_POST['download']))
{    
    $file = $_POST['download']);

    if (file_exists($file)) {
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename='.basename($file));
        header('Content-Transfer-Encoding: binary');
        header('Expires: 0');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        header('Content-Length: ' . filesize($file));
        ob_clean();
        flush();
        readfile($file);
        exit;
    }   
}
?>