将单个图像文件上传到我的目录中,并将原始文件名存储为mysql db

时间:2019-04-07 18:29:46

标签: node.js image upload multer

我无法使用multer上传图片。我要上传图像并将其存储在目录“ / uploads / images”中。我似乎无法将图像上传到所需的目的地。即使代码没有给出任何错误,图像也不会出现在“ / uploads / images”中。有人可以给我一些指导吗?谢谢!

var multer = require('multer')

var Storage = multer.diskStorage({
    destination: function(req, file, callback) {
        callback(null, "./uploads/images");
    },
    filename: function(req, file, callback) {
        callback(null, file.originalname);
    }
});

var upload = multer({
    storage: Storage
});
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())

app.post('/admin', upload.single('file'), function(req, res) {
    var start_time = convertTimeStringTo24Hours(req.body.starttime)
    var end_time = convertTimeStringTo24Hours(req.body.endtime)
    var attendee = ''
    if (req.body.attendee == '') {
        // default value for attendee
        attendee = 100
    } else {
        attendee = req.body.attendee
    }
    var hiddensignup = 0
    if (req.body.hiddensignup == 'on') {
        hiddensignup = 1
    }
    var sql =
        'INSERT INTO events' +
        ' (name, description, start_time, end_time, location, max_capacity, hidden_sign_up, other, image_path)' +
        ' VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)'
    var values = [req.body.eventname, req.body.description, req.body.date + ' ' + start_time,
        req.body.date + ' ' + end_time, req.body.location, attendee, hiddensignup, req.body.other, '/uploads/images' + req.body.file
    ]

    con.query(sql, values, function(err, result) {
        if (err) throw err
        res.redirect('/admin')
    })
})

0 个答案:

没有答案