如何通过post回调函数触发下载提示


How to trigger a download prompt through post callback function

我想这是个简单的问题,但是我想不明白…问题在这里:

在前端,我将一些用户选择的数据导出到服务器,以动态创建一个文件:

$("#export-button").click(function() {
$.post("'.$postUrl.'",{variable:exportSelection},
    function(data) {
        console.log(data);
    }
);});

然后,在我接收到数据并在服务器上创建/保存文件后,我在php中执行以下操作:

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

我应该在post回调函数中做什么,而不是在控制台上打印,以获得下载提示?

更新:OK -我刚刚意识到我的#export-button是一个锚标记没有href…现在,当我指向服务器上的文件时,问题是,当我单击时,它遵循链接提示保存文件等,但它在"新"版本生成之前到达文件(因此在每次单击时使用以前的选择)

好了,我明白了。我只需要禁用指向任何地方的链接,然后输入:

窗口。Location = 'my_file_location.doc';

…在回调函数内部