I have an endpoint called '/downloadUserAction' that collects data and downloads a csv file. The challenge I am having is that the file won't get downloaded when calling endpoint using a button on click function, however it'll download only when I access the endpoint directly in the browser.
Upon research, I've came across findings that concluded that you cannot use AJAX to download files. That makes sense, since when I click on my button I see the endpoint being hit and the file contents being passed in the network tab, however no file gets downloaded on the client.
This is all I'm simply doing on the javascript side using data tables button plug-in feature on my page to call my endpoint.
$(document).ready(function () {
var table = $("#userActivity").on('init.dt', function() {
}).DataTable({
dom: 'Blfrtip',
buttons: [
{
extend: 'csvHtml5',
text: 'NLP Search Download',
action: function ( e, dt, node, config ) {
$.ajax({
url : window.location + "/downloadUserAction?draw=3&search%5Bvalue%5D=NLP_SEARCH&order%5B0%5D%5Bcolumn%5D=6&order%5B0%5D%5Bdir%5D=desc"
});
}
}
],
Is there another method of calling my endpoint that will force a download on the client page?
side-note: my data table is using server side processing otherwise I would've just used datatables csv exporting button.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…