我有一个简单的下载excel文件的联系表单。主要问题发生,当ajax加载。我想下载excel文件然后将用户重定向到下一页..下面是我的代码与虚拟数据..
Ajax代码..
$.ajax({
type: "POST",
url: "/site/ajaxexcel.php",
data: {'value':'send'},
cache: false,
success: function(html){
location.href = '<?php echo base_url()."/site/welcome.php" ?>';
}
});
我的ajaxexcel.php
代码是:
<?php
$content= '<html xmlns:x="urn:schemas-microsoft-com:office:excel">
<head>
<!--[if gte mso 9]>
<xml>
<x:ExcelWorkbook>
<x:ExcelWorksheets>
<x:ExcelWorksheet>
<x:Name>Sheet 1</x:Name>
<x:WorksheetOptions>
<x:Print>
<x:ValidPrinterInfo/>
</x:Print>
</x:WorksheetOptions>
</x:ExcelWorksheet>
</x:ExcelWorksheets>
</x:ExcelWorkbook>
</xml>
<![endif]-->
</head>
<body><table class="table table-condensed table-striped table-hover table-bordered pull-left" id="myTable"><thead><tr><th>Rakesh</th><th>kumar</th></tr></thead><tbody><tr><th>Rakesh</th><th>Rakesh</th></tr><tr><th>Rakesh</th><th>Rakesh</th></tr><tr><th>Rakesh</th><th>Rakesh</th></tr><tr><th>Rakesh</th><th>Rakesh</th></tr></tbody></table></body></html>';
header('Content-type: application/excel');
header('Content-type: image/jpeg,image/gif,image/png');
header("Content-Disposition: attachment; filename=download.xls");
header("Pragma: ");
header("Cache-Control: ");
echo $content;
?>
我想下载excel文件,然后将用户重定向到特定位置。
如果您已正确完成,也可以帮助我使用您的codeigniter代码。
答案 0 :(得分:5)
尝试以下方法,希望它能正常工作
ajaxexcel.php
window.open
答案 1 :(得分:4)
我认为下载非常困难。请使用PHP Excel创建文件并下载。
答案 2 :(得分:4)
您无法使用Ajax请求下载文件。任何如何重定向到特定位置,允许您下载文件。您可能尝试过这个,但尝试在单独的选项卡中下载文件并将您的页面重定向到特定页面。
<input id="btnGetResponse" type="button" value="Redirect" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$("#btnGetResponse").click(function () {
window.open('/test/excel.php');
window.location = "http://stackoverflow.com/";
});
});
</script>
同样从您的PHP文件(excel.php)中删除此行。否则,它会将您的文件下载为JPG或PNG
header('Content-type: image/jpeg,image/gif,image/png');
答案 3 :(得分:4)
使用https://github.com/johnculviner/jquery.fileDownload js:
$.ajax({
type: "POST",
url: "/site/ajaxexcel.php",
data: {'value':'send'},
cache: false,
success: function(html)
{
$.fileDownload("ajaxheader.php",
{
successCallback: function (url) {
location.href = '<?php echo base_url()."/site/welcome.php" ?>';
},
failCallback: function (responseHtml, url) {
alert('error');
}
});
}
});
在php端添加标题下面的行:
header("Set-Cookie: fileDownload=true; path=/");
答案 4 :(得分:2)
您可以使用jquery.fileDownload.js
。
你可以在这里找到一个例子。 。 。 http://www.codeasearch.com/working-with-jquery-filedownload-js-plugin.html
答案 5 :(得分:2)
实际上对于这种情况,我建议使用空白选项和重定向打开文件位置。为此,您需要创建一个表单结构并将其提交给您的action.php
示例:
var form = document.createElement("form");
form.setAttribute("method", "post");
form.setAttribute("action", "action.php");
form.setAttribute("target", "myView");
// or you can use _blank : form.setAttribute("target", "_blank");
var hiddenField = document.createElement("input");
hiddenField.setAttribute("type", "hidden");
hiddenField.setAttribute("name", "message");
hiddenField.setAttribute("value", "val");
form.appendChild(hiddenField);
document.body.appendChild(form);
window.open('', 'myView');
form.submit();
alert("Redirect in 10 second!");
setTimeout(function(){
location.href = "/home"
}, 10000);
答案 6 :(得分:2)
使用https://github.com/johnculviner/jquery.fileDownload
同样从您的PHP文件(excel.php)中删除此行。否则,它会将您的文件下载为JPG或PNG
标题('Content-type:image / jpeg,image / gif,image / png');
答案 7 :(得分:2)
您可以通过创建虚拟表单并发布它来实现此目的。
function autoGenerateAndSubmitForm(method, url, post_data) {
var element = document.getElementById("virtual_form");
if(element != null )
{
element.parentNode.removeChild(element);
}
var form = document.createElement("form");
form.setAttribute("id", "virtual_form");
form.setAttribute("style", "display:none;");
form.setAttribute("target", "_blank"); // This line is very important in your case for redirect in other page and download your file.
form.method = method;
form.action = url;
for(i in post_data)
{
var element=document.createElement("input");
element.value=post_data[i];
element.name=i;
form.appendChild(element);
}
document.body.appendChild(form);
form.submit();
form.parentNode.removeChild(form);
}
在您需要的地方调用上述方法,我假设您已在点击事件中调用它
$('button').on('click', function(e){
autoGenerateAndSubmitForm("POST","/site/ajaxexcel.php",{'value':'send'});
location.href = '<?php echo base_url()."/site/welcome.php" ?>';
});
从服务器端代码中删除以下行。
header('Content-type: image/jpeg,image/gif,image/png');
答案 8 :(得分:2)
在Ajax ....只需你可以在服务器上创建一个excel文件... ajax响应将是excel文件的路径...在ajax成功打开新标签中的excel文件(它会自动下载excel)并同时打开新位置的新标签。
下面给出了ajax成功中的示例代码
window.open("path to excel..");
window.open("new location");
也可以使用(如果需要)
window.location.replace("new location");
答案 9 :(得分:2)
方法1 :使用jQuery AJAX
使用以下代码替换您的ajax ..
<script>
$.ajax({
type: "POST",
url: "/site/ajaxexcel.php",
data: {'value':'send'},
cache: false,
success: function(html)
{
window.open("/site/ajaxexcel.php");
location.href = '<?php echo base_url()."/site/welcome.php" ?>';
}
});
</script>
window.open
将在单独的窗口中打开ajaxexcel.php文件,location.href
将重定向到给定的welcome.php文件。这是最好的方法。
方法2 :使用jQuery filedownload插件
请加入 jqueryfiledownload script并执行以下操作:
<a class="download" href="/path/to/file">Download</a>
<script>
$(document).on("click", "a.download", function () {
$.fileDownload($(this).prop('href'))
.done(function () { alert('File download a success!'); //success code})
.fail(function () { alert('File download failed!');//failure code });
return false; //this is critical to stop the click event which will trigger a normal file download
});
</script>
现在当您点击锚点时,您的文件已下载,您可以分别在done()/fail()
函数中编写成功/失败代码。
答案 10 :(得分:1)
只需使用简单的javascript
即可window.location.replace(###file url###);
答案 11 :(得分:1)
1)Ajax不是解决方案。
2)window.open()
弹出窗口将在大多数浏览器中被阻止,甚至来自同一个域(至少在一段时间以前,当我尝试实现类似的东西时,在Chrome和FF中发生)
这样的事情可以完成这项任务:(它不会检查返回的代码等内容,因此404
或非文件输出将导致重定向而无需下载)
document.getElementById('zzz').onclick = function() {
ifrm = document.createElement('iframe');
ifrm.style.display = "none";
ifrm.src = "https://github.com/SheetJS/test_files/blob/b07031a7bd5a8b86b9baac94267f20cf81c6d5eb/A4X_2013.xls?raw=true";
// some random xls file from github (it contains just a single "x" character in cell A-4)
ifrm.onload = function() {
window.location.href = "https://jquery.com";
};
document.body.appendChild(ifrm);
return false;
}
&#13;
<a href="#" id="zzz">
click me
</a>
&#13;
P.S。:对于某些文件类型(如pdf),您需要application/octet-stream
Content-type
标头来强制浏览器下载文件而不是使用内置查看器。