nodejs multer express js如何检索刚刚上传/存储的图像

时间:2019-12-23 17:45:09

标签: node.js express multer

我想知道如何检索刚刚使用router.get存储的图像。任何帮助,将不胜感激!任何改善我的代码的技巧也将不胜感激!祝你有美好的一天

profile.js

var express = require('express');
var router = express.Router();
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, limits:{
    fileSize: 1024 * 1024 
} }).single('image');


router.post('/', function (req, res) {
    upload(req, res, function (err) {
        if (err) {
            // An error occurred when uploading
        }
        console.log(req.file)
        console.log(req.file.path)
        res.json({
            success: true,
            message: 'Image uploaded!'
        });

        // Everything went fine
    })
});


module.exports = router;

app.js

var express=require('express');
var app=express();
var multer = require('multer');

var bodyParser=require('body-parser');
var urlencodedParser=bodyParser.urlencoded({extended:false});

app.use(bodyParser.json()); //parse appilcation/json data
app.use(urlencodedParser);
app.use('/profile', profile);

module.exports=app;

0 个答案:

没有答案