问题描述
此 enctype =" multipart/form-data" 阻止我将数据发送到mysql,但这对于 multer 正常工作.我正在尝试将图像文件名存储到数据库,而文件将存储到我的项目目录./使用 multer 框架.我尝试了不同的查询,但没有任何反应.我希望你们遇到像我一样的问题,请花点时间.预先谢谢你.
This enctype="multipart/form-data" blocking me from sending my data to mysql but it is a must for multer to work. I'm trying to store the image filename to database while the file will store to my project directory ./uploads using multer framework. I've tried different queries but nothing happen. I'm hoping for you guys which have faced the same issue like mine please take a little of your time. Thank you in advance.
<form class="add-music-form" action="/save" method="POST" enctype="multipart/form-data">
<div class="form-group">
<div class="song-feaured-image">
<input style="display: none;" type="file" accept="image/*" id="file" name="featured_img" onchange="loadFile(event)" required>
<img id="output" class="image-preview" width="130" src="/img/rock-on.jpg" />
<label for="file" class="attach_file" style="cursor: pointer">
<i class="material-icons">attach_file</i></label>
</div>
</div>
<div class="form-group">
<input type="text" class="form-control" placeholder="Title" id="title" name="title" required>
</div>
<div class="form-group">
<input type="text" class="form-control" placeholder="Band name" id="band_name" name="band_name" required>
</div>
<div class="custom-file">
<input type="file" class="custom-file-input" id="customFile" name="audio" required>
<label class="custom-file-label" for="customFile">Select audio file</label>
</div>
<div class="mt-3">
<button type="submit" class="btn btn-primary">Submit</button>
<a onclick="cancel()" class="btn btn-default">Cancel</a>
</div>
</form>
const path = require('path');
const express = require('express');
const multer = require('multer');
const ejs = require('ejs');
const bodyParser = require('body-parser');
const mysql = require('mysql');
const app = express();
const DIR = './assets/uploads/featured-img';
// Set The Storage Engine
let storage = multer.diskStorage({
destination: function (req, file, callback) {
callback(null, DIR);
},
filename: function (req, file, cb) {
cb(null, file.fieldname + '-' + Date.now() + path.extname(file.originalname));
}
});
let upload = multer({storage: storage});
// Set Database Connection
const connection=mysql.createConnection({
host:'localhost',
user:'root',
password:'',
database:'nodejs_crud'
});
connection.connect(function(error){
if(!!error) console.log(error);
else console.log('Database Connected!')
});
//set view file
app.use(express.static('assets'));
app.use(express.static(__dirname + '/assets'));
//set view engine
app.set('view engine', 'ejs');
app.use(bodyParser.json());
// app.use(express.urlencoded({ extended: true }));
app.use(bodyParser.urlencoded({ extend: false}));
app.get('/',(req, res) => {
let sql = "SELECT * FROM music"
let query = connection.query(sql, (err, rows) => {
if(err) throw err;
res.render('music_index');
});
});
app.get('/', (req, res) => res.render('music_index'));
app.post('/save',upload.single('featured_img'), function (req, res) {
let sql = "INSERT INTO `music`(`featured_img`, `title`, `band_name`, `audio`) VALUES ('" + req.body.featured_img + "', '"+req.body.title+"', '"+req.body.band_name+"', '"+req.body.audio+"')";
let query = connection.query(sql, (err, results) => {
if(err) throw err;
res.redirect('/');
});
});
// Server Listening
const port = 3000;
app.listen(port, () => console.log(`Server started on port ${port}`));
推荐答案
建议
您插入数据库的数据不在req.body对象上,而是在其他位置.像req.files.file
对象.您可以通过在app.js文件上使用 console.log('req.file'+ req.file +'req.files'+ req.files)来打印日志
your data that inserted into database isn't on req.body object instead some where else.like req.files.file
object. you can print out the log by using console.log('req.file' + req.file + 'req.files' + req.files) on your app.js file
app.post('/save',upload.single('featured_img'), function (req, res) {
console.log('req.file' + req.file + 'req.files' + req.files)
let sql = "INSERT INTO `music`(`featured_img`, `title`, `band_name`, `audio`) VALUES ('" + req.body.featured_img + "', '"+req.body.title+"', '"+req.body.band_name+"', '"+req.body.audio+"')";
let query = connection.query(sql, (err, results) => {
if(err) throw err;
res.redirect('/');
});
});
这篇关于当表单设置为enctype ="multipart/form-data"时,我们如何在MySQL中发送数据?在node.js中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!