As the title mentions, I am trying to download a file which is served with associated mime type via PHP script given by href URL, then reload the same page, but can't quite figure it out, here's what I have so far:
<a id="viewAttachmentLink" href="/path/to/script.php?id=123">View Attachment</a>
<script type='text/javascript'>
jquery('#viewAttachmentLink').bind('click', function() {
if (myFunction()) {
window.location.href = "jquery(this).attr('href')";
setTimeout(location.reload(), 400);
} else {
return false;
}
});
</script>
With this code, it will reload the page, but appears to not make the call to the PHP script.
As the title mentions, I am trying to download a file which is served with associated mime type via PHP script given by href URL, then reload the same page, but can't quite figure it out, here's what I have so far:
<a id="viewAttachmentLink" href="/path/to/script.php?id=123">View Attachment</a>
<script type='text/javascript'>
jquery('#viewAttachmentLink').bind('click', function() {
if (myFunction()) {
window.location.href = "jquery(this).attr('href')";
setTimeout(location.reload(), 400);
} else {
return false;
}
});
</script>
With this code, it will reload the page, but appears to not make the call to the PHP script.
Share Improve this question edited Sep 21, 2018 at 10:57 informatik01 16.4k11 gold badges78 silver badges108 bronze badges asked Dec 5, 2012 at 23:37 Mike PurcellMike Purcell 20k10 gold badges54 silver badges93 bronze badges 9- What do you mean by download a file? Are you wanting to load the resource to execute something in the browser or download the file to the user's puter? Is it a typo the line setting href to a string? – Andrew Hubbs Commented Dec 5, 2012 at 23:42
-
A few oddities: 1. what is
myFunction()
? 2. it would bewindow.location.href = $(this).attr('href');
3. why do you need toreload()
when you're already setting the href to something else? – Brian Cray Commented Dec 5, 2012 at 23:44 - @AndrewHubbs: Download the file, then reload the page. – Mike Purcell Commented Dec 5, 2012 at 23:46
- @BrianCray: MyFunction is just a placeholder call indicating that I am making a call to another function which will return true/false. Didn't want to add all that code as it doesn't pertain to the question. – Mike Purcell Commented Dec 5, 2012 at 23:47
- 3 I was able to sort of resolve the situation by adding 'target=_new' to the link definition. Now when link is clicked the download will force open a new window where the remote php script will dictate header content, etc. The browser then downloads the file and closes the window, then the main window reloads. Going to leave this open if anyone has a more elegant solution. – Mike Purcell Commented Dec 6, 2012 at 0:49
2 Answers
Reset to default 2As mentioned in the ments, I was able to get around the issue by adding a target="_new"
attribute to the link. So when the link was clicked it would send the request to the remote php script to another window, which would control the headers and start downloading the file, and the original window would reload as needed.
I did a file download and status update (through a header) like this:
<a id="downloadData">Download data...</a>
<script type="text/javascript">
$('#downloadData').on('click', function () {
$.ajax({
url: '/data',
method: 'GET',
success: function (data, textStatus, request) {
var output = request.getResponseHeader('output');
//refresh page here and use the output
//console.log(output);
var a = document.createElement('a');
var url = '/data';
a.href = url;
a.click();
//save file
},
error: function (e) {
console.log(e);
}
});
});
</script>