0

I use the following route to create a excel file. The Excel is created correctly, and is created in the root folder, where also is the template.xlsx i am using.

app.get('/proceso/:id', function(req, res)
{
            res.download( __dirname + "/report.xlsx")
});

If i call the route directly in the browser, for example localhost:7555/proceso/2 the file is generated and a download starts automatically.

I want to use an ajax get request to call the route,

function reporte_excel(idp){

$.ajax({
type : "GET",
contentType : "application/json",
url : "/proceso/"+ idp,
success: function (data)
    {


    }
});

};

Now the file is generated but no download starts. Is there a way to start the download on client side inside of ajax success? Or how should the download be started on server side?

Ingolf Krauss
  • 161
  • 1
  • 2
  • 23
  • 1
    Looks like this was already answered. Please take a look at this [question](https://stackoverflow.com/questions/20830309/download-file-using-an-ajax-request). – Bohdan Sviripa Jan 30 '19 at 02:39

2 Answers2

0

Try

window.location = "URL to your file download script"

Healyhatman
  • 1,482
  • 1
  • 14
  • 23
0

To start a download with res.download( __dirname + "/public/report.xlsx") it must match the folder declared app.use(express.static(__dirname + '/public'))

so when the excel file is generated it must be generated in the matching folder.

in my case it is like this

workbook.toFileAsync("./public/report.xlsx").then(() => {
            res.download( __dirname + "/public/report.xlsx")
          })
Ingolf Krauss
  • 161
  • 1
  • 2
  • 23