I\'m trying to build a \"Export to CSV\" system using ajax so this is my ajax call
$(document).on(\'click\', \'#export-csv\', function(){
$.ajax({
Try creating a
element with href
attribute set to data URI
of file using URL.createObjectURL
, Blob
; download
attribute to set file name for "Save file" dialog
success: function (data) {
var resp = data.response;
var a = $("<a />", {
href: "data:text/csv,"
+ URL.createObjectURL(new Blob([resp], {
type:"text/csv"
})),
"download":"filename.csv"
});
$("body").append(a);
a[0].click();
}
Return the url of the .csv file in your ajax call. Then in your success function put:
location.href = url;
Just like all file types that are not naturally handled by the browser this will cause the browser to download the file.