0

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

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;
        }
        ?>  
    }}
]
});
4

1 回答 1

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;
    }   
}
?>
于 2013-03-21T21:12:35.533 回答