Wappler Methodology To Delete a CSV File After Download

Can someone help me to work out the methodology to download a CSV file and then delete the file on the server?

Here is where I have got to...
(various parameters around file names not shown for simplicity)

  1. Define a download element on the front end:
<dmx-download id="attendee_download" ...>
</dmx-download>
  1. Run a server action which ends with the Export CSV File action. On success, run the above download:
<dmx-serverconnect id="attendee_list_generate" ...
dmx-on:success="attendee_download.download()">
</dmx-serverconnect>
  1. Define another server action which will delete a file:
<dmx-serverconnect id="file_delete">
</dmx-serverconnect>
  1. Somehow run the file_delete server action once the download is complete.

My question is what dynamic event do I put on the dmx-download element to do this?

<dmx-download id="attendee_download"
dmx-on:WHAT_TO_PUT_HERE="file_delete.load({...})">
</dmx-download>

Posting screenshot here for others to see:
image

Maybe the Load? Or the Progress? But progress you only delete at 99% or 100%

You can delete a file while the download is in progress, because Linux only truly deletes the file once it's no longer open (if you want the technical explanation it's something related with inodes)

However, the correct methodology would perhaps shift the responsability to delete the file server-side, at cron for example. You would delete files older than 15 minutes or something. You could prefix the filename with the current Unix timestamp. Or use a temporary folder (also a Linux technical term), but I don't know how that works, especially with Wappler on the mix

Thanks for that @Apple !

I was expecting to find a Files -> Downloaded or Download -> Success type of option...

I typically put files in a temp folder and download from there. I then run a scheduled action to periodically empty the folder

1 Like

Hi Antony,

I recently setup a similar process where I delete the CSV file from the server after it has been downloaded. In my case, I'm using Server Connect Route to download the file instead of the dmx-download component.

There are 2 server connects - one to export/download the file (server connect route) and another to delete the CSV file on the server.

On success of the export/download SC, I run an inline flow to download the CSV and then run the file delete SC by passing the CSV's filename.

<dmx-serverconnect id="sc_export_csv" url="/api/export_csv" noload="true" dmx-on:success="run([{run:{outputType:'text',action:`browser1.goto(\'/download_file_export/\'+sc_export_csv.data.exportFileName)`}},{wait:{delay:5000}},{run:{outputType:'text',action:`sc_del_downloaded_file_export.load({file_name: sc_export_csv.data.exportFileName})`}}])"></dmx-serverconnect>
2 Likes

I've put a trash can icon on the page if a file exists and the user clicks it to delete it. This only really works for an admin area where the site owner knows that leaving a file there is a security risk so they make sure they click the icon. But I prefer the idea of doing it automatically 15 mins after it was created so you don't rely on anyone having to manually click something.

1 Like

Wow, that inline flow is pretty gnarly!
I love the idea though @guptast , and I was thinking something pretty similar this evening about having the wait, but I think I’ll use a page flow.

Thanks for your idea! :star2:

1 Like

You're welcome, Antony!

I added wait to ensure that the file was not deleted before it had been downloaded. It doesn't add any wait for the user to finish the download process because the inline flow is running after the export/download SC has been successfully executed.

1 Like

This would fail/stop the download if it takes more than 5 seconds to download.

Better option is to run a scheduler and delete files that are x-mins older, every few minutes.

Another option would require a custom JS implementation to download the file and then invoke browser download. Here, you may call the delete SC after JS download is completed, as it would not affect the browser download.

1 Like

Loving all these solutions!

@Teodor , am I right in saying there is no dmx-on:downloaded solution?