下载文件并通过ajax将其重定向到另一个页面

我有一个简单的下载excel文件的联系表格。 主要问题发生,当ajax加载。我想下载excel文件然后将用户重定向到下一页..下面是我的代码与虚拟数据..

Ajax代码..

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

我的ajaxexcel.php代码是:

 <?php $content= '    
Rakeshkumar
RakeshRakesh
RakeshRakesh
RakeshRakesh
RakeshRakesh
'; 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代码。

尝试下面的方法,希望它会工作

  1. 通过window.open在新窗口中打开ajaxexcel.php
  2. 它将开始下载,然后关闭它。
  3. 一旦关闭,重定向页面。

我认为下载非常困难。 请使用PHP Excel创建文件并下载。

您无法使用Ajax请求下载文件。 任何如何重定向到允许您下载文件的特定位置。 您可能尝试过这个,但尝试在单独的选项卡中下载文件并将页面重定向到特定页面。

    

同样从您的PHP文件(excel.php)中删除此行。 否则,它会将您的文件下载为JPG或PNG

 header('Content-type: image/jpeg,image/gif,image/png'); 

通过使用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 = ''; }, failCallback: function (responseHtml, url) { alert('error'); } }); } }); 

在php端添加标题下面的行:

 header("Set-Cookie: fileDownload=true; path=/"); 

你可以使用jquery.fileDownload.js 。 你可以在这里找到一个例子。 。 。 http://www.codeasearch.com/working-with-jquery-filedownload-js-plugin.html

实际上对于这种情况,我建议使用空白选项和重定向打开文件位置。 为此,您需要创建一个表单结构并将其提交给您的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); 

使用https://github.com/johnculviner/jquery.fileDownload

同样从您的PHP文件(excel.php)中删除此行。 否则,它会将您的文件下载为JPG或PNG

header(’Content-type:image / jpeg,image / gif,image / png’);

您可以通过创建虚拟表单并发布它来实现此目的。

 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 = ''; }); 

从服务器端代码中删除以下行。

 header('Content-type: image/jpeg,image/gif,image/png'); 

在Ajax ….只需你可以在服务器上创建一个excel文件… ajax响应将是excel文件的路径…在ajax成功打开excel文件在新选项卡(它自动下载excel)和在同时打开新位置的新标签。

下面给出了ajax成功中的示例代码

  window.open("path to excel.."); window.open("new location"); 

也可以使用(如果需要)

 window.location.replace("new location"); 

立即在Chrome中打开多个链接作为新标签

方法1 :使用jQuery AJAX

用以下代码替换你的ajax ..

  

window.open将在单独的窗口中打开ajaxexcel.php文件, location.href将重定向到给定的welcome.php文件。这是最好的方法。

方法2 :使用jQuery filedownload插件

只需包含jqueryfiledownload脚本并执行以下操作:

 Download  

现在,当您单击锚点时,您的文件已下载,您可以分别在done()/fail()函数中编写成功/失败代码。

只需使用简单的JavaScript

 window.location.replace(###file url###); 

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; } 
  click me