下载通过jQuery AJAX后不工作 [英] Downloading Via JQuery AJAX Post not working

查看:114
本文介绍了下载通过jQuery AJAX后不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过使用jQuery POST请求使用Ajax下载文件。

i want to download a file using ajax via post request using jQuery.

下面是PHP code我使用。

here is the PHP code i am using.

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);
}

和这是jQuery的

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

是不是可以下载这种方式?我尝试使用Google,有些建议是相同的方式,我这样做。但在我的控制台也显示了一些垃圾值作为响应。

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.

我在哪里去了?

感谢你..

推荐答案

您可以简单地发送POST请求到您的应用/阿贾克斯/ EXCEL /的download.php 页面表单数据,你想,忘记使用jQuery的AJAX。如果您的响应报头是正确的,当你发布你的erp_customerinvoice的形式,浏览器将默认情况下,告诉你一个下载文件对话框,你将永远不会浏览到的download.php页面,因为响应头将prevent一个重定向。

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. 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.

请参阅一些相关问题进行更多的解释和替代方案:

See some related questions for more explanation and alternatives:

  • <一个href="http://stackoverflow.com/questions/2186562/post-to-server-receive-pdf-deliver-to-user-w-jquery">POST到服务器,接收PDF,提供给用户瓦特/ jQuery的
  • Ajax响应内容处置附件
  • POST to server, receive PDF, deliver to user w/ jQuery
  • Ajax Response Content-Disposition attachment

这篇关于下载通过jQuery AJAX后不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆