Skip to content Skip to sidebar Skip to footer

Nodejs - Request - Express- Use File Readstream Inside Of A Post Handler

I'm using nodejs and express4. I have a form - post method (file upload as base64) and I'm saving this file to a gridfs with streams. this is my code: exports.transcribe = functi

Solution 1:

ok I was able to work around the problem. I'm not using the streams anymore:

exports.transcribe = function (gridfs, req, res) {

    const buffer = Buffer.from(req.body.file.split(';base64,')[1], 'base64')
    let contentType = req.body.file.split(';base64,')[0].replace('data:', '')
    streamifier.createReadStream(buffer).pipe(gridfs.createWriteStream({
        filename: uuid.v4() + '.txt',
        content_type: contentType,
        metadata: {
            user: req.user
        }
    }))
    request.post(process.env.REST_ENDPOINT, {
        headers: {
            'Content-Type': 'multipart/form-data'
        },
        formData: {
            'file': {
                value: buffer,
                options: {
                    filename: 'transcribeme.txt',
                    contentType: contentType
                }
            }
        }
    }).then((data) => {
        console.log(data)
        res.status(200).json({user: req.user})
    }).catch((err) => {
        console.error(err)
        res.status(500).json({user: req.user})
    })

}

Post a Comment for "Nodejs - Request - Express- Use File Readstream Inside Of A Post Handler"