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)

    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大