0

Long story short, I want to return a downloaded file and some information regarding the file to use it later in Front-End in a response from a POST petition, this is what I have

I'm using a success flag to see if there's nothing wrong with the file processing function, if the flag is true, i have to redirect to another page after downloading the file, otherwise display an error message.

response method on server side

res.download(pathofile)
res.status(200).send({
     success: true,
     message: "File processed Correctly!",
     filename: filename,
     numberoflines: numberoflines
})

client-side

///Call to POST petition
if(res.success){
  console.log(filename)
  console.log(numberoflines)
  alert(res.message)
}
//Do something with values...

However, after the petition ,the client side only detects the body of the response function (I'm getting the filename and numberoflines from server) and not the downloaded file. I would like to know if it is possible to send both parts of the information (the values and the file) in one response or if a have to do something else. Thanks in advance!

alejandro
  • 13
  • 3
  • You should be logging `res.filename` and `res.numberoflines` – Barmar Aug 11 '21 at 17:49
  • 1
    You can't send a regular response and a file download in the same request response. – Barmar Aug 11 '21 at 17:51
  • See https://stackoverflow.com/questions/4545311/download-a-file-by-jquery-ajax for workarounds. – Barmar Aug 11 '21 at 17:52
  • I see, so, i have to either trigger the download or send the information of the response, right? – alejandro Aug 11 '21 at 18:23
  • Right. See the linked question. A common approach is for the response to include a URL to open in a new window or iframe, and that URL performs the download. – Barmar Aug 11 '21 at 18:24

0 Answers0