从AJAX调用下载一个内置PHP输出缓冲区的文件 [英] Download a file built in PHP output buffer from AJAX call
问题描述
php:// ouput
,所以我不必担心物理文件阻塞服务器。是否可以从返回 php:// output
到AJAX启动下载?这是我的代码: HTML / jquery:
!DOCTYPE html>
< html lang =en>
< head>
< meta charset =utf-8/>
< script type =text / javascriptlanguage =javascriptsrc =jquery.js>< / script>
< script type =text / javascript>
$(#download)。live(click,function(){
var request = $ .ajax({
dataType:'html',
url: 'php.php',
success:function(response){
alert('Finished');
}
})
})
< ; /脚本>
< / head>
< body>
< h1 id =download> DOWNLOAD< / h1>
< / body>
< / html>
PHP:
code><?php
header('Content-type:application / vnd.ms-excel');
header('Content-disposition:attachment; filename =test.csv');
$ f = fopen('php:// output','w');
fwrite($ f,'this,is,a,test');
fclose($ f);
readfile('php:// output');
返回;
?>
我不知道如何让这个从AJAX调用返回一个文件保存对话框。 p>
这必须很简单,但我似乎找不到结合这两个问题的任何示例。
以下工作原理很好,因为它调用了php.php文件两次。有人有什么更好的想法吗?
<!DOCTYPE html>
< html lang =en>
< head>
< meta charset =utf-8/>
< script type =text / javascriptlanguage =javascriptsrc =jquery.js>< / script>
< script type =text / javascript>
$(#download)。live(click,function(){
var request = $ .ajax({
dataType:'html',
url: 'php.php',
success:function(response){
window.open('php.php');
}
})
})
< / script>
< / head>
< body>
< h1 id =download> DOWNLOAD< / h1>
< / body>
< / html>
有没有为这个实例缓存php.php,以便在 window.open('php.php')
,但是当我点击下载
时,会重新加载内容?
为什么 window.open(response)
不一样?
I am trying to build a CSV file in PHP, then call the PHP file from an AJAX call, which will then initiate a download of the CSV file upon success of the AJAX call. This works fine if I save a physical copy of the .csv on the server, but I would like to use php://ouput
so I do not have to worry about physical files clogging up the server. Is it possible to initiate a download from returning php://output
to AJAX? Here is my code:
HTML/jquery:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<script type="text/javascript" language="javascript" src="jquery.js"></script>
<script type="text/javascript">
$("#download").live("click", function() {
var request = $.ajax({
dataType: 'html',
url: 'php.php',
success: function(response) {
alert('Finished');
}
})
})
</script>
</head>
<body>
<h1 id="download">DOWNLOAD</h1>
</body>
</html>
PHP:
<?php
header('Content-type: application/vnd.ms-excel');
header('Content-disposition: attachment; filename="test.csv"');
$f = fopen('php://output', 'w');
fwrite($f,'this,is,a,test');
fclose($f);
readfile('php://output');
return;
?>
I am not sure how to get this to return a File Save dialog from my AJAX call.
This has to be simple, but I can't seem to find any examples that combines these two issues.
The following works, but is highly inneficient as it calls the php.php file twice. Does anybody have any better ideas?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<script type="text/javascript" language="javascript" src="jquery.js"></script>
<script type="text/javascript">
$("#download").live("click", function() {
var request = $.ajax({
dataType: 'html',
url: 'php.php',
success: function(response) {
window.open('php.php');
}
})
})
</script>
</head>
<body>
<h1 id="download">DOWNLOAD</h1>
</body>
</html>
Is there anyway to cache 'php.php' for just this instance so that it loads instantly under window.open('php.php')
, but will reload contents when I click download
next?
Why does window.open(response)
not work the same?
这篇关于从AJAX调用下载一个内置PHP输出缓冲区的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!