I have a button that, when clicked, triggers the downloadFile
function which contacts the backend to download a file.
async downloadFile(name) {
await this.$axios.$get(process.env.API_LINK + '/api/files/' + name)
},
app.get('/api/files/:name', (req, res) => {
res.download('files/' + req.params.name, function (error) {
console.log('Error : ', error);
});
})
Upon clicking the button, the console displays an error as undefined
. However, if I access the API directly through the browser (
localhost:9000/api/files/teste.pdf
), the file is downloaded successfully.
The correct file name is being passed to the API when the button is clicked.
Edit (14.01.24, 19h36):
I updated the code based on Express 4.x - API Reference (specifically the err
section) and included path.join()
. Now, there are no errors displayed. However, nothing happens either - no file downloads or errors reported.
I can still manually download the file using the URL
localhost:9000/api/files/teste.pdf
in the browser, but when trying to initiate the download through a button (which calls the downloadFile()
function), nothing occurs.
.get('/files/:name', (req, res) => {
res.download(path.join(__dirname, '..', 'files', req.params.name), function (err) {
if (err) {
console.log(err);
}
});
})