简体   繁体   English

Node.js:Multer上传有承诺吗?

[英]Node.js: Multer upload with promise?

I have this express route with multer file-upload. 我有这个带有multer文件上传的快速路由。 When the upload is complete, I would like to encode the image to base64 and send with response. 上传完成后,我想将图像编码为base64并发送响应。

However when I do it like this, the code tries to execute the base64 encoding before the file is created to the folder. 但是当我这样做时,代码会尝试在将文件创建到文件夹之前执行base64编码。

Edit : Added storage & upload functions 编辑 :添加了存储和上传功能

const storage = multer.diskStorage({
    destination: (req, file, callback) => {
        if (!fs.existsSync('./uploads')) {
            fs.mkdirSync('./uploads');
        }
        let path = './uploads';
        callback(null, path);
    },
    filename(req, file, cb) {
        let fileExt = file.originalname.substring(file.originalname.lastIndexOf('.')).toLowerCase();
        if (!imageFilter(fileExt)) {
            return false;
        } else {
            cb(null, file.originalname);
        }
    },
    onError: function (err, next) {
        console.log('error', err);
        next(err);
    },
});

const upload = multer({
    storage,
    limits: {
        fileSize: 1000 * 1000 * 2 // 2 MB
    }
}).single('file');

router.post('/upload', function (req, res) {
    var directory = 'uploads';
    fs.readdir(directory, (err, files) => {
        if (err) throw err;
        for (var file of files) {
            fs.unlink(path.join(directory, file), err => {
                if (err) throw err;
            });
        }
    });
    upload(req, res, function (err) {
        if (err) {
            return res.status(404).json({
                success: false,
                message: 'File is too large. (Max 2MB)'
            });
        }

        var file = req.file;
        var base64str = base64_encode('./uploads/' + file.originalname);

        return res.status(200).json({
            success: true,
            url: 'http://' + ip.address() + ':' + constants.PORT + '/api/uploads/' + file.originalname,
            image: 'data:image/png;base64,' + base64str
        });
    });
});

What would be the smartest way to achieve the right order of operations. 什么是实现正确运营顺序的最明智的方法。 Possibly promises or async/await? 可能是承诺还是异步/等待?

This solution worked for me : 这个解决方案对我有用:

Node v8.4.0 is required for this 此节点需要节点v8.4.0

//app.js
const fs = require('fs');
const express = require('express');
const cors = require('cors');
const bodyParser = require('body-parser');
const app = express();

app.use(cors({credentials: true, origin: 'http://localhost:4200'}));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

const Uploader = require('./Uploader.js');
const uploader = new Uploader();

app.post('/upload', uploader.startUpload);


//Uploader.js

const util = require("util");
const crypto = require("crypto");
const multer = require('multer');

class Uploader {

    constructor() {
        const storageOptions = multer.diskStorage({
            destination: function(req, file, cb) {
                cb(null, __dirname + '/uploads/')
            },
            filename: function(req, file, cb) {
                crypto.pseudoRandomBytes(16, function(err, raw) {
                    cb(null, raw.toString('hex') + Date.now() + '.' + file.originalname);
                });
            }
        });

        this.upload = multer({ storage: storageOptions });
    }

    async startUpload(req, res) {
        let filename;

        try {
            const upload = util.promisify(this.upload.any());

            await upload(req, res);

            filename = req.files[0].filename;
        } catch (e) {
            //Handle your exception here
        }

        return res.json({fileUploaded: filename});
    }
}

Edit : The library " util " provide you a " promisify " method which will give you the possibility to avoid something called the "callback hell". 编辑 :库“ util ”为您提供了一个“ promisify ”方法,它可以让您避免被称为“回调地狱”的事情。 It converts a callback-based function to a Promise-based one. 它将基于回调的函数转换为基于Promise的函数。

This is a small example to understand my code above: 这是了解上述代码的一个小例子:

const util = require('util');

function wait(seconds, callback) {
  setTimeout(() => {
    callback();
  }, seconds);
}

function doSomething(callType) {
  console.log('I have done something with ' + callType + ' !');
}

// Default use case
wait(2 * 1000, () => {
  doSomething('callback');
});

const waitPromisified = util.promisify(wait);

// same with promises
waitPromisified(2000).then((response) => {
  doSomething('promise');
}).catch((error) => {
  console.log(error);
});

// same with async/await

(async () => {
  await waitPromisified(2 * 1000);
  doSomething('async/await');
})();

Will print : 将打印:

I have done something with callback !
I have done something with promise !
I have done something with async/await !

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM