0

I have a cfml page where I display the data and have the ability to download it. I would want to run the page to view the data and when clicked to download it without again hitting the DB to fetch the data. Is this possible to view and download only with one single request to DB?

Chaitu
  • 29
  • 5
  • Yes, but if they are two separate page requests, then you would need to either temporarily store the query result in a persistent scope so that you can reuse it on the download request, or use a short `cachedwithin` to cache the query result so that CF does not go back to the DB at the next attempt if your SQL is identical. And if you do either approach then you would need to be confident that your server has the memory to handle the size*quantity of such queries for the time you persist them. In the case of using application/session scope then clean up after yourself to avoid a memory leak. – Sev Roberts Feb 02 '21 at 18:29
  • 2
    @SevRoberts I'd recommend the `cachedwithin` approach long before storing it in `application` or `session` scope. – Adrian J. Moreno Feb 02 '21 at 18:43
  • Another approach would be to write the data to a file right after you get it from the db. You would then need a way to get rid of old files. – Dan Bracuk Feb 02 '21 at 19:19
  • 1
    Yes, this is possible to do, you will just need to write a little JavaScript. See this question here and you can look at several different solutions https://stackoverflow.com/questions/15547198/export-html-table-to-csv – user12031119 Feb 02 '21 at 21:05
  • Thank you all for the suggestions. I will try to implement it using your recommendations. – Chaitu Feb 03 '21 at 21:42
  • In the past I put the result in the desired format into a hidden form field on the page where the "Export" button was the form submission. The submission sends the data to the server for rendering into a file. You could just use JS most likely to generate the file too, but in either case another query wouldn't have to be run. My form solution was simple, easy, and guaranteed to work in all browsers. – Leeish Feb 10 '21 at 20:01

0 Answers0