Download from frontend as user


#1

Hi.
I’m pretty new in development and adonisjs is where I’m building my first large project.

I would like to make it possible for the users to download the data (ex. a list of all the users) from the frontend as a csv or xml file. Is there any built in functions in adonisjs to do this, or does anyone know a library or solution to do this?


#2

Hi)
I guess you can use something like this in your controller:

response.attachment('path-to-you-file.csv')

More details here: https://adonisjs.com/docs/4.1/response#_attachments


#3

Hi! I have the data I want the user to be able to download as a .csv file and I have converted my JSON response from the database to .csv format, then I want to create a file, save it to Helpers.tmpPath(some path) and then it gets downloaded to the user wit response.attachment(Helpers.tmpPath(some path)). Any solutions anyone?


#4

Solved it like this:
convert JSON to csv with javascript function

then

response.header('Content-type', 'text/csv')
response.header('Content-Disposition', 'attachment; filename="users.csv"')
response.send(csvString)