Categories
JavaScript Answers

How to export JavaScript data to a CSV file without server interaction?

Spread the love

Sometimes, we want to export JavaScript data to a CSV file without server interaction.

In this article, we’ll look at how to export JavaScript data to a CSV file without server interaction.

How to export JavaScript data to a CSV file without server interaction?

To export JavaScript data to a CSV file without server interaction, we can set a link to a base64 string with the CSV content.

For instance, we write

const csvString = csvRows.join("\r\n");
a.href = "data:application/csv;charset=utf-8," + encodeURIComponent(csvString);

to create the csvString with the CSV data.

Then we put the data in a base64 string with

"data:application/csv;charset=utf-8," + encodeURIComponent(csvString)

And we assign the value to a.href where a is an a element.

Then when we click on the link, the data will be downloaded.

Conclusion

To export JavaScript data to a CSV file without server interaction, we can set a link to a base64 string with the CSV content.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *