'Unable to render PDF to browser when retrieved from gridfs-stream

My Controller : -

 view: async (req, res) => {

        var id = req.params.pid
        id = mongoose.Types.ObjectId(id)

        const project = await Project.findById(id)

        const upid = project.uploadedGridFileId
        var uploads = mongoose.createConnection("mongodb://localhost:27017/techv-uploads", { useNewUrlParser: true, useUnifiedTopology: true })
        //console.log(uploads)

        uploads.once('open', function () {
            var gfs = gridfsStream(uploads.db, mongoose.mongo)
            var readstream = gfs.createReadStream({
                _id: mongoose.Types.ObjectId(upid),
                root: "projectPdfs"
            })

            //console.log(readstream)
            res.set("Content-Type" , "application/pdf")
            res.set("Content-Disposition", "inline" )
            readstream.pipe(res)
        })
    }

Instead to directly displaying on the browser , it gives download option . It is working with image files but not with pdfs .



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source