I have a web form that users complete online. When they press submit it will start a file download for them.
At the moment, I process the form submission and generate a suitable file for the user and fire it off with suitable headers. eg...
header('Content-type: "application/octet-stream"');
header('Content-Disposition: attachment; filename="yourfile.txt"');
header("Content-Transfer-Encoding: binary");
However, since this starts a download right away, the original form is left on screen by the browser.
I would really like to go to some "Thank you" screen once the download completes (or before the download starts). I know it is possible, because almost every download site you visit does this (normally to pump you full of adverts before the download starts).
So, How do I show a "Thank You" screen that starts the download after a second?
How would any solution proposed effect the behaviour of the back button, as I don't want the file downloading again without the form being refilled?
I am using PHP on the server and can rely on Javascript (and jQuery) being available on the client.
Thank you.
You could send the form to the Thank you document and put there a META refresh to the file download:
<meta http-equiv="refresh" content="3;url=download.php">
<p>Thank you! The download will start in 3 seconds. If not, use this link to download the file</p>
Add a second page that says something "thank you, your donwnload will start in a few seconds" and triggers the download using javascript:
$(document).ready(function(){
window.setTimeout(function(){
window.location = 'http://yourdownloadhost.com/file.zip';
}, 1500);
});
or use a meta redirect.
You can insert a hidden iframe into your page and submit your form to this iframe.
Related
I'm trying to make a product overview page where a user can download an invoice. However this invoice needs to be generated to the specific product.
At the moment the way i do this is to navigate to invoice.jsp which sends the ID to the server which in turn creates the invoice.pdf, after this is created the user can push a button to download the just created file.
However I would like to skip the last step and trigger the download as soon as the page is loaded and redirect back to the orderoveriew.jsp
Make an iframe on orderoveriew.jsp with the pdf code. Just remember to set
header('Content-Type: application/force-download');
and it will download automatically, other methods might not work with pop-up blockers.
I have a PHP script that downloads a PDF file from the server and prompts you to either open it, or save it. The script accepts a one time token, which is used in place of a file name, to hide the file name.
If you go to the actual php page, http://example.com/files/download/token the script works fine and it downloads the PDF.
I could just send people to that page with a standard link tag, but once the file downloads I need to update content on that page which is returned through that download script.
Is there any way to have ajax call open up a new window where the file will download and then return the data that I need to update the current page?
There is more to the download script, but the main piece is the actual downloading part:
header("Content-type: application/pdf");
$this->load->helper('file');
readfile("static/temp_statements/".$local_file_name);
unlink("static/temp_statements/".$local_file_name);
One trick that I have used in the past that might be useful to you is
Supply a query param in your ajax call to download the PDF. This will be a unique name.
The server process that streams the PDF for download sets a cookie with this unique name.
You poll in your page waiting for this cookie to appear.
When the cookie appears you can assume the file has downloaded, and you can do your contingent action.
And you do not need to open a window to make this happen. You could just append an invisible iframe like this:
$(some selector).append($("<iframe width='1' height='1' frameborder='0' src='" + url + "'></iframe>"));
In the interests of honesty and transparency, I originally found this idea from this SO answer and it worked for me: Detect when browser receives file download
I want to allow a user to enter a date range and then be able to download an Excel file with data retrieved based on that date range.
Initially I had this working by submitting the form as a GET request for the same page. If the GET params are present the page will retrieve the data and output the needed headers and then the data. I would then exit; from the process to prevent the page from loading (since I had to send the headers with the Excel data).
This works. However I want to include a progress indicator that will go away once the data has been sent. However I cannot find a way to do this.
I can start such an indicator when the form submit button is pressed, but I cannot find a way to turn it off once the document had been output for download.
Ajax does not work in this sort of situation, btw.
Can anyone suggest how to do this? I've seen suggestion here on SO about loading the output into a hidden iframe but I do not understand how to do this myself.
Because I have no code on which I could base I will write you the theory mostly.
user requests to download a file
you load a page where inside you are creating the file, the process takes some time, during which you show the user a progress bar.
instead of throwing this file to the user with header function (which you cannot use as you already are printing on the screen) you save the file into a temp directory. preferably call it with a random name, or a timestamp.
by the end of file creation print on the screen a JavaScript command like this:
<script language=”JavaScript”>
self.location=”download.php?file=whatever_you_called_it.xls”;
</script>
set the header for the Excel download with something like this:
header('Content-disposition: attachment; filename=your_excel_file.xls');
header('Content-type: application/xls');
readfile('tmp_dir/whatever_you_called_it.xls');
unlink ('tmp_dir/whatever_you_called_it.xls');
its always good to clean the things after yourself. be aware that it is possible that something does not get deleted. So preferably you would have a garbage collector anyways. And keep files out of public directories
Obviously, if the data is not sensitive, then you can simply set javascript directly to the file and the browser will download it directly. But then you would need some kind of garbage collector to clean all these files that are created... it would be messy...
I have url to PDF file. I need to have a link on the page so that when the user clicks the link, they see the "Save As" dialog.
I found a solution using an iframe, but if the user has installed a PDF plugin, the "Save As" dialog does not show up.
Is there any other way to show the user the "Save As" dialog?
Sorry, I forgot to mention that after user will click on link, request will be sent by ajax. It will be post request for a PDF file url. In the result of this request I will know there is PDF file is located, and now I would like to show the user Save as dialog instead of opening it in browser.
There is a PHP way, you need to redirect your user to a php file that will include the content and pass on some headers:
header('content-type: application/x-pdf');
header('content-disposition: attachment; filename=yourfile.pdf');
readfile('yourfile.pdf');
This should overcome most if not all weird browser initiatives. If you still get the pluggin instead of the save dialog, there isn't much more you can do IMO.
Good luck
You don't want to send the POST via ajax. Instead, POST to the iframe as described in this answer to the question you found, and use the Content-Dispostion header to tell the browser what to do with it as described in this answer to the question you found.
(Marked this "community wiki" because the question is really just a duplicate.)
I have found numerous examples on forcing he download of a file with PHP, but none of them show how to do this from a new page (e.g. a thank you for downloading page, with a button to start download if it didn't start already). When ever I use the header attachment method it just downloads the file whilst leaving the browser on the page I was on when I click the link.
I want to be able to show a new page and automatically start the download. How do I do this with PHP?
It can be done by meta refresh on the "Thank you page" that you want where 5 is in seconds, change it to be a longer delay if you want.
<META HTTP-EQUIV="Refresh" CONTENT="5; URL=/download.php">
Then your download script where you're pointing at URL=/download.php should do the force download to what ever file it needs to serve.