小编Hos*_*mil的帖子

Busboy 文件事件未触发

所以,我有这个帖子请求,它曾经工作正常,但是一旦我升级到 Node 10,我似乎无法弄清楚为什么文件事件完全停止触发......

router.post('/uploadImage', authenticate.FBAuth, (req, res) => {
    const busboy = new BusBoy({ headers: req.headers });
    let imageFileName;
    let imageToBeUploaded = {};

    busboy.on('file', function onFile(fieldname, file, filename, encoding, mimetype) {
        console.log('onFile started');
        
        if (mimetype !== 'image/png' && mimetype !== 'image/jpeg') {
            return res.status(400).json({ error: 'Unsupported file format' });
        }

        const imageExtension = path.extname(filename);
        imageFileName = `${Math.round(Math.random() * 100000000000)}.${imageExtension}`;
        console.log(imageFileName);

        const filepath = path.join(os.tmpdir(), imageFileName);
        console.log(filepath);

        imageToBeUploaded = { filepath, mimetype };
        console.log(imageToBeUploaded);

        file.pipe(fs.createWriteStream(filepath));
    });

    busboy.on('finish', function onFinish() {
        admin …
Run Code Online (Sandbox Code Playgroud)

javascript file-upload node.js express busboy

4
推荐指数
1
解决办法
1239
查看次数

标签 统计

busboy ×1

express ×1

file-upload ×1

javascript ×1

node.js ×1