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.)
Related
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 am working on a php project. I am getting a filename as the response of my webservice.
There is a link i am showing on the page on success of the webservice call.Onclick of the link
i need to open a dialogue box where it will ask the user to save or open the file. On selection of save as, it will save the file in a particular location and if we select open, it will open the file. I am totally unaware as how to do this. Can any body help me out with some examples or any pointers?
Thank you
the best method would be to just stream the file from PHP, and let the browser show the default save/open dialog.
if you need more advanced behavior, you'll need a lot of javascript.
I noticed that on facebook, when you click on an image it opens up in their photo viewer. I understand that is done with javascript, but when you look at the url it says:
http://www.facebook.com/photo.php?fbid=3230830261587&set=a.1664092454121.85323.1591534315&type=1&theater
But, you can still see your profile/or news feed (wherever you were when you clicked on the image) behind the photo viewer. How? It appears (upon observation of the url) that you have been redirected to facebook.com/photo.php. Is that the case? And how are you redirected back to www.facebook.com/(previous).php when you close the viewer?
Facebook uses a new browser feature that can modify the browser's history. See http://badassjs.com/post/840846392/location-hash-is-dead-long-live-html5-pushstate for how Facebook changes the URL in the address bar. To display the photo, all that Facebook does is show a DIV over the document body at the same time.
I have a "calculator" page that calculates a load of maths based on what the user inputs. The user can then click "get results" to show their full calculations.
I then have a button - "save as pdf" which I want it to allow the user to save a pdf of their results.
It's a combination of html, php, css combined with Wordpress.
How can I (on click) convert the results to a pdf and allow the user to save?
p.s I know this been asked a few times but I can't seem to find the correct answer or a working version... or a tool with good documentation for me to follow!
Check also FPDF
Generally these things are solved on the server. Clicking the link would trigger a redirect to the php page and in general instead of writing output to a webpage, you write output to a pdf stream (still arrives in the browser but the browser knows it as a pdf).
you do this by changing the response headers. you could use this library to write out the pdf file
http://code.google.com/p/wkhtmltopdf/ make sis pretty simple, you could do a curl request to the page, and send it to the library.
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.