In my web app, the user can download file that are stored in the server. At the moment, when a file doesn't exist, another page open with just the response of the server. I would like now to have this response displayed in an alert box.
Server-side, this is the code that send the file for download, if it exists
router.get('/download/file', function(req, res){
const file = `${__dirname}/uploads/Environnement/file.pdf`;
try {
if (fs.existsSync(file)) {
res.download(file);
} else {
res.send("File does not exist!")
}
} catch (e) {
console.error(e);
}
});
For now, the code that allow to get the file from the server was like this :
<form action="/HSE/Environnement/download/file" method="get">
<button type="submit" name="button">Download</button>
</form>
And I try to do like below :
function getFile(url) {
fetch(url).then(function(response) {
response.text().then((text) => {
alert(text);
})
});
}
The problem is that when the file exist, it doesn't download it.
I tried also to send different status, but on client-side I always have the same.
router.get('/download/file', function(req, res){
const file = `${__dirname}/uploads/Environnement/file.pdf`;
if (fs.existsSync(file)) {
res.status(200).download(file);
} else {
res.status(404).send("File does not exist!")
}
});
File exist
Response {type: "basic", url: http://localhost:8001/HSE/Environnement/download/file", redirected: false, status: 200, ok: true, …}
File doesn't exist
Response {type: "basic", url: "http://localhost:8001/HSE/Environnement/download/file", redirected: false, status: 200, ok: true, …}
Any idea ?