Force download PDF created by TCPDF, after Ajax post submit - php

My application allow the users to complete a form, and send it with an Ajax post call.
The form si sent to the TCPDF class that create a PDF file.
The class has the method "->output(...)" that permit to save the file into web server, send it to the browser, ecc...
my goal is, after the form submit, create the PDF file, and force the user to download it (with no refreshing).
The method doesn't work with ajax calls.
The only solution I've found is create the file, seve it into web server, and than redirect the user to the location of the file to the web server; but it isn't a nice solution, I need to force the download (auto starting).
I've read others similar solution on the forum, but thay aren't good form
Any suggestions?

$pdf->Output("filename.pdf",'D');

Try
$pdf->Output("filename.pdf",'FD');
This will suggest client if he wants to save or open file.

Related

How to force CSV download when it reaches the client-side? (PHP)

I am trying to serve up a CSV file as the response to a get request.
If I go onto Dev tools -> Network I can see the CSV values in the response preview, and if I double click that it will download the file. However I can't seem to get it to download automatically.
I have messed around with every type of header I can think of.
Any ideas?
Yes, It seems you can't get file downloads from ajax requests (Thanks to Quentin for the linked answer in the comments), at least not easily anyway.
This is because there is a subtle separation between the user and the browser. Ajax is triggered by the browser so the file is returned to the browser not the user.
A form submit on the other hand is triggered by the user and so the file is returned to the user (automatically downloading).
So in order to fix this I changed to a form with a post method with hidden inputs and it now works a treat.
Thanks all.

How to resume a download when the request contains POST data?

I have a php file which generate a file to be downloaded using POST parameters. I already have everything working to resume file downloading (using Range header and everything related).
However, if I pause the download and then try to resume it, the browser does not send POST data in the request. This works fine using GET data instead but I'd like to keep using POST. How could I make this work ?
Notes:
The file is generated on the fly and sent to the browser (simply print'ed by php) using the right headers.
I cannot save the file somewhere and serve it. It has to be served on the fly.
*sorry for my bad english.
well, lets say that you have database to save the POST params.
then you need to create unique url for the download based on the params.
lets say that the download link is dowload.php <- you send the POST here.
now you need to save the params there, and lets says that you get the unique_id.
after that you redirect the page to the new page(with the unique_id param) that process the download, for example resume_download.php
example download url will be resume_download.php?req=[your_unique_id]
or you can use .htaccess to made the url more friendly
this is not guarantee the download will continue, but at least user don't need to re-enter the form.

Upload large files to Amazon S3 directly and add other date to database using web forms and PHP

I want to create a single web form where a user provides a large file, and some information like title and description. When the form is submitted, the file should be directly uploaded to Amazon S3 (without uploading it to the web server), while the information is added to the database using PHP.
How would I go about doing this? I know I could use the direct upload post solution, but then I wouldn't be able to add the other information to the database.
I would implement an ajax submit of the file on S3 (using the direct upload parameters, eventually) and then submit all the other fields to your webserver in a second step (even not ajax, of course).
You could also use an hidden field in your form to submit the new S3 link (which you can retrieve after the ajax upload is completed), since I guess you need to store it together with all the other data.

PHP Keep the User's File Upload Path After Validation Fail

i want to ask that if its possible to keep the user's upload path where he selects from his computer that which file will be uploaded like the path " C:\Users\User1\Desktop\1.jpg " , the reason i want to learn about this is after submitting forms with php when he fails about validation, i want him to not reselect it..
Summary : How can i save the path : C:\Users\User1\Desktop\1.jpg in user's form if user fails on other field's validation
Thanks
It is not possible. Only the filename is sent by the browser, not the full path.
First thoughts
I am just posting this in case you think of doing something crazy like, why don't I use javascript or JS library to get the path?
It's a DOM element after all.
Well browsers are your enemy!! why? For security reasons, browsers have security restructions like as you have guessed don't expose the local file structure.
Question
What would you do with the file path?
How's a file path going to be handy on the server-side?
Hem, let me think...still thinking.....
The only purpose that I can think of is discovery before attempting a hacking.
Field validation can be an honest answer, but you can avoid this by creating a form that uses AJAX to submit the data to sever.
What??
Yes sir, use ajax submit the data as post, validate it and send a message back to the browser stating if the form was successfully submitted or not.
With ajax you don't leave the page when data is submitted to the server

sending and receiving files with PHP jQuery

How can I send and receive files live like in yahoo chat? i.e if I send the file than the user on the other end will only get that file if he click on the accept button and if he click deny than the file should not upload and should be deleted from the server... I want to do that via PHP jQuery $.ajax().
This is a rather complicated question, and not easily addressed. Here are a couple things:
You'll want a way to identify conversations. You wouldn't want your file-request getting intercepted by a completely different set of people.
You'll probably want to invoke some form of long-polling that will send a request to the server, who in turn will send a request (link) to the other user, who, when clicks the link, the server will return the first request back to you, initializing your upload.
You can use the jQuery Plugin Uploadify for the asynchronous uploads, and late-initialization from a server-response.
You may also want to keep a database table to list all of the file shared by any particular conversation. This would be queries to find out if new files need to be listed in the users windows.

Categories