Express.js: TypeError: path argument is required to res.sendFile

Issue

I am uploading my files in a directory using Multer.

I want to download those same files which I have uploaded as I am displaying them in the form of a table on the template with each file having a download option. Here’s my code:

Express.js

router.get('/downloadfile', (req, res, next) => {
    var options = {
        root: path.join(__dirname, './uploads'),    //all my files are saved in uploads folder
        dotfiles: 'deny',
        headers: {
            'x-timestamp': Date.now(),
            'x-sent': true
        }
    }

    var fileName = req.query.id3
    res.sendFile(fileName, options, function (err) {
        if (!err)
        {
            console.log('File sent successfully');
        }
        else
        {
            console.log('Error occurred while sending file.' + err.message)
        }
    })
});

Angular

onDownloadFiles(i: any)
{
    this.fileToDownload = i;
    console.log(this.fileToDownload);

    const params = new HttpParams().set('id3', this.fileToDownload);
    this.http.get('http://localhost:3000/downloadfile', {params})
    .pipe(map(responseData => { return responseData; }))
    .subscribe(response => {
            console.log(response);
    })
}

Here is the error while clicking on the download button.

TypeError: path argument is required to res.sendFile 

Solution

Well, you can use modern try-catch methods when you’re programming with asynchronous javascript rather than using conventional if-else statements to log errors.

Secondly, instead of using a callback function inside res.sendFile it is better to check whether the file exists and then send it.

Here’s a sample code.

module.exports.getExe = async (req, res) => {
    try {

        const file = getExeFilePath(req.params.filename)

        if (!fs.existsSync(file.path)) {

            res.status(200).json({ 'status': false, 'result': 'File not found!' })

        } else {

            res.setHeader('Content-disposition', 'attachment; filename=' + file.name);
            //filename is the name which client will see. Don't put full path here.

            res.setHeader('Content-type', file.type);

            var sendFile = fs.createReadStream(file.path);

            sendFile.pipe(res);
        }
    } catch (error) {
        console.log(error)
        res.status(200).json({ 'status': false, 'result': 'Failed!' });
    }
}

Answered By – Srinath Kamath

Answer Checked By – Timothy Miller (AngularFixing Admin)

Leave a Reply

Your email address will not be published.