I have a Rails API endpoint which sends a csv file on API request:
def download_csv
headers = ['column one', 'column two', 'column three']
csv_data = CSV.generate(headers: true) do |csv|
csv << headers
csv << ["A", "B", "C"]
end
csv_data.strip
send_data(
csv_data,
filename: 'attachment.csv',
type: :csv,
disposition: 'attachment; attachment.csv',
)
end
Problem:
When i hit the endpoint, it returns a 200, however my browser isn't downloading the file...
Note that i am running a Rails API server so I don't have any views only Mode controller and services.
my frontend code looks like this:
<Button
onClick={() => {
return API.downloadUserCsv().then(() => {
Modal.alert({ body: 'Success' })
})
}}
>
Download CSV
</Button>
downloadUserCsv(): Promise<AxiosResponse> {
return this.http.get(`/admin/debug/users/download_csv`)
}
Found the solution.
<Button
onClick={() => {window.open(your backend url)})
>