将数据发送到php并下载文件
我需要发送数据到php文件并下载它 当我直接调用脚本时,它工作正常,但当我使用AJAX发送数据时,它根本不会下载 我的问题是:如何将数据发送到php文件并自动下载文件,但当然保持在同一页面上 直接调用时工作的部分代码 PHP文件将数据发送到php并下载文件,php,javascript,jquery,download,Php,Javascript,Jquery,Download,我需要发送数据到php文件并下载它 当我直接调用脚本时,它工作正常,但当我使用AJAX发送数据时,它根本不会下载 我的问题是:如何将数据发送到php文件并自动下载文件,但当然保持在同一页面上 直接调用时工作的部分代码 PHP文件 header('Content-Description: File Transfer'); header("Content-type: application/ms-word"); header("Content-Disposition: attachment;Fil
header('Content-Description: File Transfer');
header("Content-type: application/ms-word");
header("Content-Disposition: attachment;Filename=ponuda.doc");
$productsArr = json_decode($_POST['object']);
$html = "<tr>";
foreach($productsArr as $product)
{
//something
}
....
echo $html;
我做了以下工作:
theBuffer=new交换器('dwnld')代码>
<iframe name="dwnld" id="dwnld" style="width:0;height:0;border:0">
</iframe>
Note: the id value assigned to the iframe is the same value given in step 2 when initializing.
注意:分配给iframe的id值与初始化时步骤2中给出的值相同。
结果是用户从不离开当前页面下载任何数量的文件,因为实际下载是在单独的页面(也称为iframe)中处理的。我已经在我所有的项目中使用它很多年了。我认为你不能通过ajax将标题发送到浏览器,但是你可以使用这个,很棒的东西
您必须重定向到下载页面,如果标题设置正确,并且浏览器识别出它正在下载文件而不是显示页面,则这将起作用。这就像点击一个.exe链接,你的浏览器不会显示白色页面——它只是统计下载。Ajax不适用于文件下载,但这是不可能的
function downloadFile(){
// you can add parameters to the function as needed to pass in dynamic data sent to the back end download handler
data = "http://your_backend_file_download_handler.php?param1=val1¶m2=val2&etc=whatever"; // send whatever data you need to the php program via query string parameters
theBuffer.sendData(data); // initiate the file download
}
<iframe name="dwnld" id="dwnld" style="width:0;height:0;border:0">
</iframe>
Note: the id value assigned to the iframe is the same value given in step 2 when initializing.
$.fileDownload('file.mp3')
.done(function () { alert('File download a success!'); })
.fail(function () { alert('File download failed!'); });