dpqy77560 2015-07-02 11:32
浏览 35

从AJAX调用时,下载文件功能不起作用?

This might be a bad question, because it's possible the issue is to something else in my project, but I was hoping that changing the header from an AJAX call may be an issue someone is familiar with. The below PHP function executes fine when called synchronously, but does not download anything and returns a 200 error when I call it from AJAX.

  public function download_csv() {

    $csv = 'hello, header';
    header('Content-Disposition: attachment; filename="format.csv"');
    header('Content-Type: text/plain');
    header('Content-Length: ' . strlen($csv));
    header('Connection: close');


    echo $csv;
  }
  • 写回答

1条回答 默认 最新

  • doucheng3407 2015-07-02 11:43
    关注

    In my knowledge you cannot download a file using ajax (security reasons).
    I googled a bit again, and it seems that this is the point.

    A working solution could be using an iframe that is not visible to the user, and setting its 'src' attribute to the download url when you need to download the file.

    You can also check this answer here that uses a jquery plugin and looks promising (haven't tried it myself)

    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!