var Category = require("…/junedb/category.js"); var multer = require(‘multer’); var gm = require(‘gm’); var imageMagick = gm.subClass({ imageMagick : true }); var logoName = ‘’; var storage = multer.diskStorage({ //设置上传后文件路径,uploads文件夹会自动创建。 destination: function (req, file, cb) { cb(null, ‘upload/category’) }, //给上传文件重命名,获取添加后缀名 filename: function (req, file, cb) { var fileFormat = (file.originalname).split("."); logoName = file.fieldname + ‘-’ + Date.now() + “.” + fileFormat[fileFormat.length - 1]; cb(null, logoName); } }); //添加配置文件到muler对象。 var upload = multer({ storage: storage }).single(‘cat_logo’);
exports.imgUpload = function (request, response) { upload(request, response, function (err) { if (err) { // 发生错误 response.json({ errno: 1, message: ‘上传失败!’ }); return } var path = request.file.path; //获取用户上传过来的文件的当前路径 imageMagick(path) .resize(150, 150, ‘!’) //加(’!’)强行把图片缩放成对应尺寸150*150! .autoOrient() .write(‘upload/images/’+request.file.filename, function(err){ console.log(request.file); if (err) { console.log(err); } }); response.json({ errno: 0, message: ‘上传成功!’, logoName: logoName }); // 一切都好 }) }
问题解决了,是ImageMagick的安装问题 安装时必须勾选"Install legacy utilities(e.g. convert)"选项,不安装、安装imageGagick.exe版本不对或安装选项不对时都会报“{ [Error: Command failed: aaЧaaaa ] code: 4, signal: null }”错。