问题
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..
回答1:
Try below method, hope it will work
- open
ajaxexcel.php
in new window viawindow.open
- it will start downloading, then close it.
- As soon as it closes, redirect page.
回答2:
I think it is very tough to download. Please Use PHP Excel to Create file and download.
回答3:
You can't download file using Ajax request. Any how you have to redirect to particular location which will allow you to download file. Might be you tried with this, but try to download file in separate tab and redirect your page to particular page.
<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>
Also from your PHP file (excel.php) remove this line. Otherwise it will download your file as JPG or PNG
header('Content-type: image/jpeg,image/gif,image/png');
回答4:
By using 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');
}
});
}
});
And at php side add below line in headers:
header("Set-Cookie: fileDownload=true; path=/");
回答5:
you can use jquery.fileDownload.js
for this.
you can find an example here . . . http://www.codeasearch.com/working-with-jquery-filedownload-js-plugin.html
回答6:
Actually for this situation i recommend open file location with blank option and redirect. For this purpose you need create a form structure and submit it to your action.php
Example:
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);
回答7:
By using https://github.com/johnculviner/jquery.fileDownload
Also from your PHP file (excel.php) remove this line. Otherwise it will download your file as JPG or PNG
header('Content-type: image/jpeg,image/gif,image/png');
回答8:
You can achieve this by creating virtual form and post it.
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);
}
Call above method where you need it, i assume uyou have call it in click event
$('button').on('click', function(e){
autoGenerateAndSubmitForm("POST","/site/ajaxexcel.php",{'value':'send'});
location.href = '<?php echo base_url()."/site/welcome.php" ?>';
});
Remove below line from your server side code.
header('Content-type: image/jpeg,image/gif,image/png');
回答9:
In Ajax....Simply you can create an excel file on server ... ajax response will be the path of the excel file...On ajax success open the excel file in new tab (it automatically downloads excel) and at the same time open new tab with new location.
sample code inside ajax success given below
window.open("path to excel..");
window.open("new location");
also can use (if needed)
window.location.replace("new location");
Open multiple links in Chrome at once as new tabs
回答10:
Method 1 : with jQuery AJAX
Replace your ajax with the following code..
<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
will open the ajaxexcel.php file in separate window and location.href
will redirect to given welcome.php file.This is the best method for this.
Method 2 : with jQuery filedownload plugin
Just include jqueryfiledownload script and do something like this:
<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>
Now when you click on anchor your file is downloaded and you can write your success/failure code in done()/fail()
functions respectively.
回答11:
Just use with simple javascript
window.location.replace(###file url###);
回答12:
1) Ajax is not a solution.
2) window.open()
popup will be blocked in most browsers even from same domain (at least that was happening in chrome and FF some time ago, when I tried to implement something similar)
Something like this would do the job: (It doesn't check stuff like code returned, so 404
or non-file output will cause a redirect without downloading)
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;
}
<a href="#" id="zzz">
click me
</a>
P.S.: You will want application/octet-stream
Content-type
header for some file types (like pdf) to force browser to download file instead of using built-in viewers.
来源:https://stackoverflow.com/questions/39139828/download-a-file-and-redirect-it-to-another-page-via-ajax