Best way to return a user-generated file, AJAX or Forms?
I'm new to web programming, so I need some help.
I am writing a custom file-creation app for my site. A user visits the page, clicks on some various options and toggles some checkboxes, and the presses a 'download now' link.
I have a PHP backend which will be processing the submission, and generating a PDF file. After the user presses the download link, I want the download to start like it would for any static link.
My question 开发者_开发问答is: What is the best way to do this? From my limited understanding, I have a choice between using AJAX or somehow using forms to submit the data. What are the advantages/disadvantages of each? Does anyone have any good links to examples?
Thanks
Actually you cannot download a pdf via XMLHttpRequest (AJAX).
In general you should simply redirect the user to the resource that will generate the PDF with the proper MIME type, and then leave it up to the browser to figure out how to handle it.
AJAX will not work here, because you cannot use AJAX to download the file.
Instead, you should make a normal form, and have the PHP backend serve a file download when the form is submitted.
Just use a form. Ajax provides no benefits and a lot of complications under these circumstances. (You could use it, although you would have to use a non-XHR implementation, but it would be pointless to do so)
精彩评论