I have created a custom button. When user click on that button, it will redirect to a visualforce page. There I have written some code to download CSV file.
Code:
const rows = [
["name1", "city1", "some other info"],
["name2", "city2", "more info"]
];
let csvContent = "data:text/csv;charset=utf-8,";
rows.forEach(function(rowArray) {
let row = rowArray.join(",");
csvContent += row + "\r\n";
});
var encodedUri = encodeURI(csvContent);
var link = document.createElement("a");
link.setAttribute("href", encodedUri);
link.setAttribute("download", "mydata.csv");
link.setAttribute("target", "_self");
document.getElementById('success').appendChild(link);
link.click();
Above code will get invoked from a button click. When I use this page from Classic, it is working good. But, when I use in Lightning, a corrupted file is downloaded (Refer Attachment).