I have a link on the website to a php file that generates native excel file on a fly ant outputs it directly to browser via headers for user to to open/save. Since it takes some time for the file to be generated I'd like to use jQuery Ajax to make the call and use some loading animation in the mean while.
The only thing I'm not sure how to do is how to output the file into the browser after Ajax call? Is it even possible?
(N.B. This is a paraphrasing of @dmitry's answer, but just elaborated upon)
The problem you have is that there is no means of directly returning a file to the user via AJAX - the browser has to request the file using a normal, synchronous HTTP request.
To solve this, your PHP will need to:
file_put_contents()
or similar).Your JS, on receiving this response will then need to:
window.open()
(or redirect in the current tab/window by setting location.href
).If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With