返回自动下载JQuery的AJAX调用 [英] Return automatic download JQuery Ajax Call

查看:139
本文介绍了返回自动下载JQuery的AJAX调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用的是阿贾克斯JQuery的命令调用生成一个CSV文件,并返回一个指向该文件供用户下载一个PHP脚本。

I use the Ajax JQuery command to call a PHP script that generates a CSV file and returns a link to that file for the user to download.

我想使这更人性化自动开始下载,这样用户看到的必须点击下载链接的浏览器保存或打开窗口代替。

I would like to make this more user friendly by automatically starting the download so the user sees the browsers "Save or Open" window instead of having to click the download link.

我猜我需要以某种方式改变头在阿贾克斯成功的回调函数?

I'm guessing I need to somehow change the headers in the Ajax success callback function??

我真的不知道我应该加标题我的搜索或者即使这是可能的。

I'm not really sure what I should be titling my searches or even if this is possible.

谢谢!

推荐答案

您可以做到这一点最简单的(也可能是唯一的)服务器端,不需要AJAX,像这样的:

You can do this easiest (and possibly only) server-side, no need for ajax, like this:

<?php
header('Content-type: "text/csv"; charset="utf8"'); //adjust encoding if needed
header('Content-disposition: attachment; filename="fileNameHere.csv"');
//output document in response
?>

有人觉得免费的,如果语法编辑此关的话,它已经相当长一段时间,因为我有一个PHP项目。

Someone feel free to edit this if the syntax if off, it's been quite a while since I've had a php project.

这篇关于返回自动下载JQuery的AJAX调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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