Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save macielportugal/793025954b2ac6205dec6b46f9bdca5c to your computer and use it in GitHub Desktop.
Save macielportugal/793025954b2ac6205dec6b46f9bdca5c to your computer and use it in GitHub Desktop.
Force download of csv response from ajax request
$.ajax({
url: "path/to/csvData",
type: "GET",
dataType: "text",
success: function (data) {
csvData = 'data:application/csv;charset=utf-8,' + encodeURIComponent(data);
$("#exportsags").attr({
"href": csvData,
"download": "sag_data.csv"
});
}
}); // ajax
@macielportugal
Copy link
Author

var encodedUri = 'data:application/csv;charset=utf-8,' + encodeURIComponent(data);
var link = document.createElement("a");
link.setAttribute("href", encodedUri);
link.setAttribute("download", "relatorio.csv");
link.innerHTML= "Download do Relatório";
document.body.appendChild(link);
link.click();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment