Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download a file and redirect it to another page via ajax

I have a simple contact form for download excel file . Main issue happen , When ajax load .I want to download excel file then redirect user to a next page.. Below is my code with dummy data..

Ajax code..

$.ajax({     type: "POST",     url: "/site/ajaxexcel.php",      data: {'value':'send'},     cache: false,     success: function(html){         location.href = '<?php echo base_url()."/site/welcome.php" ?>';                      } }); 

And my ajaxexcel.php code is:

<?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; ?> 

I want to just download excel file and then redirect user to a specific location.

Also you can help me with your codeigniter code if you have done it properly..

like image 295
Kumar Rakesh Avatar asked Aug 25 '16 08:08

Kumar Rakesh


People also ask

How do I redirect a page in Ajax?

ajax({ type: 'POST', url: 'b. php', data: 'result='+$name, success: function() { window. location. href = "profile.

Can we download file using Ajax?

We cannot download the file through Ajax, must use XMLHttpRequest.

How do I move to another page in Ajax?

$. ajax({ type: 'POST', url: 'AJAX URL', data: "YOUR DATA" // don't forget to pass your csrf_token when using post success: function(data){ $("what_ever_you_want_to_replace"). html(data. view); }, error: function(xhr, type, exception) { // if ajax fails display error alert alert("ajax error response type "+type); } });

Can you redirect an Ajax request?

Add a middleware to process response, if it is a redirect for an ajax request, change the response to a normal response with the redirect url. Then in ajaxComplete, if the response contains redirect, it must be a redirect, so change the browser's location.


1 Answers

Try below method, hope it will work

  1. open ajaxexcel.php in new window via window.open
  2. it will start downloading, then close it.
  3. As soon as it closes, redirect page.
like image 162
shyammakwana.me Avatar answered Oct 07 '22 23:10

shyammakwana.me