ExpressJS Multer:将图像上传到服务器

时间:2015-03-01 19:22:16

标签: express multer

我对Node.js和Express.js更新。

我想首先将图像上传到服务器(目录:uploads / spots),然后(同步)上传MongoDB中的其余表单数据。

我使用REST(Method Post)

  

app.route(' / spots')。post(users.requiresLogin,spots.create);

我正在使用Multer将图像更新到服务器中,然后工作。



app.use(multer(
        { dest: './public/uploads/spots',
            onFileUploadStart: function (file) {
                var imagePath = file.path;

                gm(imagePath).resize(850, 850).quality(70).noProfile().write('public/uploads/spots/850x850/'+file.name, function (err) {
                    if (!err) {
                        gm(imagePath).resize(150, 150).quality(70).noProfile().write('public/uploads/spots/150x150/'+file.name, function (err) {
                            if (!err) {
                            }
                            else{
                                console.log('Error: '+err);
                            }

                        });
                    }
                    else{
                        console.log('Error: '+err);

                    }

                });

            }

        }));




正在运行,但是是异步的,并且在将图像上传到服务器之前将响应返回到前端。

我的问题是如何执行此操作但同步以及如何在上传图像后将响应返回到前端。

谢谢!

spots.server.routes.js



'use strict';

module.exports = function(app) {
    var gm = require('gm');
    var multer  = require('multer');


    var users = require('../controllers/users.server.controller.js');
    var spots = require('../controllers/spots.server.controller.js');


    //Upload image
    app.use(multer(
        { dest: './public/uploads/spots',
            onFileUploadStart: function (file) {
                var imagePath = file.path;

                gm(imagePath).resize(850, 850).quality(70).noProfile().write('public/uploads/spots/850x850/'+file.name, function (err) {
                    if (!err) {
                        gm(imagePath).resize(150, 150).quality(70).noProfile().write('public/uploads/spots/150x150/'+file.name, function (err) {
                            if (!err) {
                            }
                            else{
                                console.log('Error: '+err);
                            }

                        });
                    }
                    else{
                        console.log('Error: '+err);

                    }

                });

            }

        }));



	// Spots Routes
	app.route('/spots')
		.get(spots.list)
		.post(users.requiresLogin, spots.create);

	app.route('/spots/:spotId')
		.get(spots.read)
		.put(users.requiresLogin, spots.update)
		.delete(users.requiresLogin, spots.hasAuthorization, spots.delete);

	// Finish by binding the Spot middleware
	app.param('spotId', spots.spotByID);
};




spots.server.controller.js (创建方法)



'use strict';

/**
 * Module dependencies.
 */
var mongoose = require('mongoose'),
	errorHandler = require('./errors.server.controller.js'),
	Spot = mongoose.model('Spot'),
	_ = require('lodash'),
    fs = require('fs');



/**
 * Create a Spot
 */
exports.create = function(req, res) {
	var spot = new Spot(JSON.parse(req.body.spot));
	spot.user = req.user;

    if(req.files.file)
        spot.image=req.files.file.name;
    else
        spot.image='default.jpg';


	spot.save(function(err) {
		if (err) {
            fs.unlinkSync('public/uploads/spots/'+spot.image);
            fs.unlinkSync('public/uploads/spots/850x850/'+spot.image);
            fs.unlinkSync('public/uploads/spots/150x150/'+spot.image);
			return res.status(400).send({
				message: errorHandler.getErrorMessage(err)
			});
		} else {
            var socketio = req.app.get('socketio'); // tacke out socket instance from the app container
            socketio.sockets.emit('spot.created.'+spot.municipality,  {spot:spot, user:req.user});
            socketio.sockets.emit('spot.created.'+spot.province,  {spot:spot, user:req.user});
            socketio.sockets.emit('spot.created.'+spot.community,  {spot:spot, user:req.user});
            socketio.sockets.emit('spot.created.'+spot.country,  {spot:spot, user:req.user});

            res.jsonp(spot);
		}
	});


};


/**
 * Spot authorization middleware
 */
exports.hasAuthorization = function(req, res, next) {
	if (req.spot.user.id !== req.user.id) {
		return res.status(403).send('User is not authorized');
	}
	next();
};




1 个答案:

答案 0 :(得分:3)

解决方案不是使用onFileUploadStart方法,而是在控制器中使用带回调的函数。

<强>路由

  // Spots Routes
    app.route('/spots')
    .get(spots.list)
    .post(users.requiresLogin,multer({ dest: './public/uploads/spots'}), spots.create);

<强>控制器

exports.create = function(req, res) {

if (req.files.file)
    exports.uploadImage(req.files.file,callback);
else
    callback();


    function callback(){
        var spot = new Spot(JSON.parse(req.body.spot));
        spot.user = req.user;

        if (req.files.file)
            spot.image = req.files.file.name;
        else
            spot.image = 'default.jpg';


        spot.save(function (err) {
            if (err) {
                fs.unlink('public/uploads/spots/850x850/'+spot.image);
                fs.unlink('public/uploads/spots/150x150/'+spot.image);
                return res.status(400).send({
                    message: errorHandler.getErrorMessage(err)
                });
            } else {
                var socketio = req.app.get('socketio'); // tacke out socket instance from the app container
                socketio.sockets.emit('spot.created.' + spot.municipality, {spot: spot, user: req.user});
                socketio.sockets.emit('spot.created.' + spot.province, {spot: spot, user: req.user});
                socketio.sockets.emit('spot.created.' + spot.community, {spot: spot, user: req.user});
                socketio.sockets.emit('spot.created.' + spot.country, {spot: spot, user: req.user});


                req.spot = spot;
                Feedback.subscribeSpot(req);

                Notify.getLocalSubscriptors(spot.municipality,spot.province,spot.community,spot.country,function(subscriptions){
                    Notify.create(req,null,spot,null,null,null,subscriptions,'spots/'+spot._id,false,'SPOT_CREATED', function(){
                        res.jsonp(spot);
                    });
                });
            }
        });
    }

};


exports.uploadImage = function(file, fn){
  var imagePath = file.path;

            gm(imagePath).resize(850, 850).quality(70).noProfile().write('public/uploads/spots/850x850/'+file.name, function (err) {
                if (!err) {
                    gm(imagePath).resize(150, 150).quality(70).noProfile().write('public/uploads/spots/150x150/'+file.name, function (err) {
                        if (!err) {
                          if(fn)fn();
                        }
                        else{
                            console.log('Error: '+err);
                        }

                    });
                }
                else{
                    console.log('Error: '+err);

                }

            });

};