简体   繁体   中英

How to implement download CSV file in Spring Boot, JSP, JavaScript, MySQL database on button click?

I am new to Spring boot and I have created a web application (ie an ecommerce web application). In my JSP page, I have two buttons one for upload CSV file and other button is to download data from database into CSV format. I have done uploading CSV file and I am unaware of downloading thing. Any suggestions or tutorials would be appreciated.

Here is a small function that you can use in JS. Just replace the csvContent by your CSV previously uploaded or exported from the DB.

const downloadCSVExample = () => {

  const csvContent = `data:text/csv;charset=utf-8,Column1,Column2
Row1Data1,Row1Data2
Row2Data1,Row2Data2`;

  const encodedUri = encodeURI(csvContent);
  const link = document.createElement("a");
  link.setAttribute("href", encodedUri);
  link.setAttribute("download", "myFile.csv");
  document.body.appendChild(link);
  link.click();
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM