Search code examples
javascriptcsvvue.jspapaparse

how to download a csv generated from papaparse?


I have successfully managed to unparse a json to csv using papaparse. I have stored the generated csv in a variable. On a button click, the csv is being generated and I can see the result in my console. I want to download that csv generated into my local system when I click the button. How do I do that?

const csv = Papa.unparse(csvData);


Solution

  • Try this code

    var csv = Papa.unparse(array);
    
    var csvData = new Blob([csv], {type: 'text/csv;charset=utf-8;'});
    
    var csvURL = window.URL.createObjectURL(csvData);
    
    var testLink = document.createElement('a');
    testLink.href = csvURL;
    testLink.setAttribute('test', 'test.csv');
    testLink.click();