我有一个简单的联系表格,用于下载 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 代码..
最佳答案
尝试下面的方法,希望能成功
- 通过
window.open
在新窗口中打开ajaxexcel.php
- 它将开始下载,然后关闭它。
- 关闭后立即重定向页面。
关于jquery - 下载文件并通过ajax将其重定向到另一个页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39139828/