简体   繁体   English

通过JQuery AJAX Post下载无法正常工作

[英]Downloading Via JQuery AJAX Post not working

i want to download a file using ajax via post request using jQuery. 我想使用jQuery通过post请求使用ajax下载文件。

here is the PHP code i am using. 这是我正在使用的PHP代码。

if (file_exists($file)) {
    header("Cache-Control: public");
    header("Content-Description: File Transfer");
    header("Content-Disposition: attachment; filename=$file");
    header("Content-Type: application/zip");
    header("Content-Transfer-Encoding: binary");
    readfile($file);
}

and this is the jQuery. 这是jQuery。

$('button.erp_ci_download').click(function(){
    var formData = $('form#erp_customerinvoice').serialize();
    $.ajax({
        type: 'POST',
        url:  "App/Ajax/Excel/Download.php",
        data: formData
    });
});

is it not possible to download it this way? 是不是可以这样下载? i tried googling and some suggested it is the same way i am doing it. 我尝试使用谷歌搜索,一些人建议它与我这样做的方式相同。 but in my console it shows some garbage values as response. 但在我的控制台中,它显示了一些垃圾值作为响应。

where am i going wrong? 我哪里错了?

thank you.. 谢谢..

You could simply send a POST request to your App/Ajax/Excel/Download.php page with the form data you wish and forget about using jQuery's AJAX. 您可以使用您希望的表单数据向您的App/Ajax/Excel/Download.php页面发送POST请求,并忘记使用jQuery的AJAX。 If your response headers are correct, when you post your "erp_customerinvoice" form, the browser will, by default, show you a download file dialog and you will never navigate to the Download.php page because the response headers will prevent a redirect. 如果您的响应标头是正确的,当您发布“erp_customerinvoice”表单时,默认情况下,浏览器将显示下载文件对话框,您将永远不会导航到Download.php页面,因为响应标头将阻止重定向。

See some related questions for more explanation and alternatives: 有关更多解释和替代方案,请参阅相关问题:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM