0

I have a download summary page that loads after a users selects specific elements to include in the csv file. I would like to show the user the summary page and automatically start the download. Since both the summary and the csv export require much of the same code I have it on the same page. I am appending to the post array after the initial load with a reference code. So if the post array contains a reference code it executes the code for the csv if not it executes the code for the summary display. I am doing this with an ajax post on page load. Everything works great except the fact that it returns the raw data stream for the csv and does not put it into a file. Does anyone have any ideas on how I can accomplish this?

Also I should mention my initial form is very complex and has many nested fields so looping through the post array and creating hidden fields is not a good solution.

Thanks for any help on this.

user3167249
  • 972
  • 2
  • 11
  • 27
  • 1
    I think you're over complicating this. Just change the page location to the path to the csv file (on whatever event) and the page will instantly show a file dialog prompt without changing the page and keeping all form settings in place. – Ohgodwhy Apr 14 '14 at 03:02

1 Answers1

0

Try to do a window open in javascript than an ajax call. This will make the browser try to download the CSV than trying to read the CSV in javascript. The browser will stay on the same page (summary page) when the download starts.

Joshua Bixler
  • 531
  • 2
  • 6
  • I have tried to load the ajax results in a new window but same results. Is that what you are saying? – user3167249 Apr 14 '14 at 03:02
  • Yes that is what I am saying. If you are outputting the CSV from php, you need send headers. http://stackoverflow.com/questions/398237/how-to-use-the-csv-mime-type – Joshua Bixler Apr 14 '14 at 03:11
  • Yes this is my csv export code. but it still does not work. // Export Export Array header('Content-Type: application/excel'); header('Content-Disposition: attachment; filename="'.$this->request->post['run_id'].'.csv"'); $file = fopen("php://output","w"); foreach ($export as $design){ foreach ($design as $line){ fputcsv($file,explode(',',$line)); } } fclose($file); die; – user3167249 Apr 14 '14 at 03:14
  • Try to send Content-Type: text/csv as the content type then application/excel. The browser will be trying to find a file with .xls or .xl then .csv. – Joshua Bixler Apr 14 '14 at 03:17