href pour download un file excel

Mon server génère des files excel dynamicment. J'utilise AJAX pour download le file Excel dynamic. Dans le callback de réussite, je reçois datatables du file Excel.

$.ajax({ url: exporting.action, headers: { "Authorization": "Basic " + btoa("key : " + key) }, type: "post", success: function(res){ //res is the excel file that needs to be downloaded //I know we can download image using anchor tag but not sure about excel file }, data: { 'Model': JSON.ssortingngify(modelClone) } }); 

Veuillez suggérer comment utiliser ces données dans l'atsortingbut href de l'label d'ancrage pour le téléchargement?

Remarque:

1) J'ai besoin d'AJAX pour l'autorisation d'en-tête

Améliorez votre request en ajoutant dataType: "binary" et responseType: "arraybuffer" propriété responseType: "arraybuffer" .

 $.ajax({ url: exporting.action, headers: { "Authorization": "Basic " + btoa("key : " + key) }, type: "post", responseType: "arraybuffer", dataType: "binary", success: function(res){ //res is the excel file that needs to be downloaded //I know we can download image using anchor tag but not sure about excel file }, data: { 'Model': JSON.ssortingngify(modelClone) } }); $ .ajax ({ $.ajax({ url: exporting.action, headers: { "Authorization": "Basic " + btoa("key : " + key) }, type: "post", responseType: "arraybuffer", dataType: "binary", success: function(res){ //res is the excel file that needs to be downloaded //I know we can download image using anchor tag but not sure about excel file }, data: { 'Model': JSON.ssortingngify(modelClone) } }); 

Ensuite, vous recevez un tampon de réseau qui peut être facilement téléchargé via Blob et l'URL de l'object.

 var blob = new Blob([arraybuffer], {type: "application/vnd.ms-excel"}); var objectUrl = URL.createObjectURL(blob); window.open(objectUrl); 

dans ton cas:

 $.ajax({ url: exporting.action, headers: { "Authorization": "Basic " + btoa("key : " + key) }, type: "post", success: function(res){ var blob = new Blob([res], {type: "application/vnd.ms-excel"}); var objectUrl = URL.createObjectURL(blob); window.open(objectUrl); }, data: { 'Model': JSON.ssortingngify(modelClone) } }); $ .ajax ({ $.ajax({ url: exporting.action, headers: { "Authorization": "Basic " + btoa("key : " + key) }, type: "post", success: function(res){ var blob = new Blob([res], {type: "application/vnd.ms-excel"}); var objectUrl = URL.createObjectURL(blob); window.open(objectUrl); }, data: { 'Model': JSON.ssortingngify(modelClone) } });