D
D
Dmitry2020-09-09 21:46:54
Express.js
Dmitry, 2020-09-09 21:46:54

How to download file by name using express js?

There is a code:

app.get('/downloadJson', (req, res, next) => {
    let path = './data/';
    let fileName = 'name.json';

    res.download(path, fileName);
});

I want to send the name of any file that is in the folder in res.download (path, fileName) when requested. Please tell me how to do it.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2020-09-09
@Tim308

Googled the solution:

app.get('/downloadJson/:file(*)', (req, res) => {
    let file = req.params.file;
    let fileLocation = path.join('./data', file);

    console.log(fileLocation);
    res.download(fileLocation, file);
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question