Last active
August 7, 2019 09:29
-
-
Save wholypantalones/8983865 to your computer and use it in GitHub Desktop.
Force download of csv response from ajax request
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$.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 |
This will trigger the two time request
Also It will add the whole data in the HTML.
Thanks!
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();
Thanks, this worked for me!
What happen if we have a large data set. Download failed if i have data around more than 18K rows.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
what if I have a web api and the constructor awaits a (string pattern, string format) stuff.