0

If I have:

-- public
-- csv
exampleCSV.csv

i.e. /public/csv/exampleCSV.csv

and then:

fetch('csv/exampleCSV.csv')
    .then(response => {
      console.log(response);
      response.blob().then(blob => {
        let url = window.URL.createObjectURL(blob);
        let a = document.createElement('a');
        a.href = url;
        a.download = 'exampleCSV.csv';
        a.click();
      });
    });

Why do I not get the CSV file on click but instead the html page of the react app?

Rogelio
  • 910
  • 5
  • 14
  • 1
    Does this answer your question? [The create-react-app imports restriction outside of src directory](https://stackoverflow.com/questions/44114436/the-create-react-app-imports-restriction-outside-of-src-directory) – Dan Philip Bejoy Nov 24 '22 at 04:26
  • Hello Dan, to a degree, so Create React App poses an artificial limitation. So, then I would fetch from the Src directoy? That seems, well - I'll test - but it seems screwy. OR, I have to import and drop the fetch... Not sure. We'll see. (tks for the tip - if you have any other data - pls let me know) – Rogelio Nov 24 '22 at 04:28

2 Answers2

0

Probably would be better to close this... But the way I resolved was generating the csv file in code. Then downloaded on click...

Rogelio
  • 910
  • 5
  • 14
0

If excel file is already in public folder then this usage would not be solution for your problem?

<a href="/csv/exampleCSV.csv" download>
 Download
</a>
dogukyilmaz
  • 585
  • 5
  • 11