dongpin3794 2011-12-02 14:33
浏览 106
已采纳

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

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

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

and this is the 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..

  • 写回答

1条回答 默认 最新

  • dongwaner1367 2011-12-02 14:36
    关注

    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:

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?