Button Download CSV File

I have not found updated tutorials to be able to create (in a php page containing a dynamic records table) a button to export a CSV or better still XLS file
If I understood correctly I have to create a Server Action containing

  • a Database Query action (filtered) of the table to export
  • an Export CSV File action. to create the file to export
  • a File Upload action to save the file in the "upload" folder

Then, you need to create a button on the page to download the file

I have tried various solutions, but it does not work for me.
Obviously I am the one who is wrong but I do not understand where

Hello @Marzio
I've facing some issues with download button
I will try to recreate an issue scenario for a bug report.

This is some workaround that maybe can help you out:

How many complications for a procedure that should be easy
I then read your suggestions calmly
But one thing that leaves me perplexed
I did a test.
I manually uploaded a file conto_agenzia.csv to the server's upload folder
I only tried to use the download, but it gives me an error in the connection

Well, step by step my friend!

The 500 error can easily be seen by:

The 403 is a forbidden error, so, it's the file on a restricted folder? Or a public one?

I already use the debugger
The problem is currently setting the correct path, because this is the error I find
I also created a "download" folder with all the writing permits, positioned in the main folder of the site
I am trying the correct connection

hey @Marzio

I suggest go step by step to see where your api is throughing the error

  1. Disable 2 and 3
    Check the query output on Dev...Is it OK?

Then

  1. Enable 2
    Check the exported CSV and see on Dev for errors...Is it OK?

Then

  1. Enable 2

Step 1
Database Query, after delete Chrome cache, is OK


Schermata 2024-07-27 alle 10.14.36

Step 2 ERROR


This the file export.php

So,
step1, your query returns NO RESULTS and you try to export an empty query to CSV...

Why?

  1. if the query should always return data, check your query setup

  2. if there is a case that no data will be found, you should add a condition step and check if the query has data... If there are data, then export to CSV.

That's what I would do..
Hope it helps

The fact is that the query had filters configurable on the table page.
Leaving the problem of the filters at the moment, if I remove them from action the step 2 regularly saves the CSV file in the "download" folder of the server

1 Like

Now a doubt about Action Wappler choice
For download which action must be used
what differences

Schermata 2024-07-27 alle 10.53.38

The "File Download"..

The "Download File" is for S3 Connector

Ok, I also thought it was, but I didn't understand what the other was for
I inserted the action (step3) but it produces a white page

It’s already explained in the docs how to download a file:
https://docs.wappler.io/t/file-downloads-with-server-connect/12780

For downloading the exported csv, use the path returned from the csv export step.

2 Likes

Thanks Teodor, always decisive, but thanks also to the friends who supported me
Now I finally managed to overcome the various mistakes and now the export of the query in a CSV file is ok

However, there remains a problem that I submit to Teodor
The procedure worked only by eliminating the references to the filters, which are decided on the page
My need, however, is to be able to change the list by acting on a Datepicker filter (this already works very well) but the exported file reports the total of records, because I deleted the filters from the query used by the export action.
I hope I have explained the problem well

Hi Marzio,

How are you providing information for the date filter to CSV query? Please show us how the filters ($_GET) have been setup in the CSV query and the event that is triggering this export query.

Check again the conditions you have on your database query...

One by One...
One or more of the conditions makes your query to rerurn no result...
Check them one at a time (like we did with the 1,2,3 steps above)

This is the condition that works very well on the table.
Choosing a period, the list is modified
But does not produce the same result on the exported file
In practice it should work like this:

  • You can select a period between two dates
  • The list changes
  • Click on Export CVS
  • ok

Please show us your condition...

We cannot guess what could be wrong buddy :slightly_smiling_face:

It seems the information of filtered dates is not being sent to the CSV export query.

In export action the query hace this codition
Schermata 2024-07-27 alle 15.45.32

The period values ​​are determined by a RangeDatePicker on the page
This works fine for the table view on the page, but not for the exported CSV file