简体   繁体   中英

Generate/download CSV and redirect

I'm trying to figure out what the best approach would be to dealing with a form submission which involves updating db records and generating/downloading a CSV file, before redirecting to the original calling page. I've seen posts saying that javascript is one way of dealing with this, but what would you recommend as good practice?

Here is the basic setup:

User checks a set of checkboxes which should be updated in the DB and also downloaded in CSV format. Form submits to form handling script, which updates the DB and following all updates, echoes out the CSV content (using content-disposition and all necessary headers) - this all works fine to here, but i also need to return the user to the original page, with the updates having been made. Is this possible?

You need to set a session variable in the script that updates the db records, telling that on next page load, you want to send the CSV file to the user; Then in the page with the list of the records (the original page), you check if that session variable is set. If it's set, you then redirect the user to a page that generates the CSV (with the content-disposition etc.)

Redirecting to a page that forces a download for user (via content-disposition attachment) will not take the browser away from the original page, so even after the user gets the download pop-up for the CSV file, he/she will remain on the original page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM