夏普不调整单个图片的大小

时间:2019-06-26 19:10:01

标签: multer sharp

这是我的node.js app.js 后端代码:

const sharp = require('sharp');

app.post('/images', upload.single('file'), function (req, res, next) {
       sharp(req.file.path).resize(20,20).toFile(req.file.path, (err, info) => { console.log(err) });
       res.send(req.file.filename);
})

res.send(req.file.filename)可以工作,但是图片没有调整大小,我不知道为什么。

这是我的multer app.js 参数:

var multer  = require('multer');
var storage = multer.diskStorage({
    destination: function (req, file, cb) {
        cb(null, './uploads/')
    },
    filename: function (req, file, cb) {
        cb(null, file.originalname+ '-' + Date.now()+'.jpg')
    }
});
var upload = multer({ storage: storage });

这是错误:

Error: Cannot use same file for input and output
    at Sharp.toFile (C:\UwAmp\www\geretonequipesnode\node_modules\sharp\lib\output.js:43:32)
    at C:\UwAmp\www\geretonequipesnode\app.js:77:40
    at Layer.handle [as handle_request] (C:\UwAmp\www\geretonequipesnode\node_modules\express\lib\router\layer.js:95:5)
    at next (C:\UwAmp\www\geretonequipesnode\node_modules\express\lib\router\route.js:137:13)
    at Immediate._onImmediate (C:\UwAmp\www\geretonequipesnode\node_modules\multer\lib\make-middleware.js:53:37)
    at runCallback (timers.js:706:11)
    at tryOnImmediate (timers.js:676:5)
    at processImmediate (timers.js:658:5)

谢谢

EDIT:已解决:

var fs = require('fs');
app.post('/images', upload.single('file'), function (req, res, next) {
       sharp(req.file.path).resize(20,20).toBuffer(function(err, buffer) {
            fs.writeFile(req.file.path, buffer, function(e) {
            });
        });
       res.send(req.file.filename);
})

0 个答案:

没有答案