0

How to download file with data which comes from server in text/csv format?

I'm using fetch. I have function like this

download(content, filename, contentType)
    {
        const a = document.createElement('a');
        a.href = 'data:text/csv;charset=utf-8,' + encodeURI(content);
        a.target = '_blank';
        a.download = filename;
        a.click();
    }

Response comes like object.

Hope your help!

Андрей Гузюк
  • 2,134
  • 6
  • 29
  • 53

1 Answers1

0

bruteforce method: replace the filename extension with .csv and then send it to your client. You can achieve that with php. replace filename extension with php

good solution : if you want to convert a file to csv, you will need a rather powerful api. Maybe your best bet is to read the file yourself if you know what youre expecting, and generate the CSV.

Max Alexander Hanna
  • 3,388
  • 1
  • 25
  • 35