在nodejs中,有一个使用multi上传的图片文件。我想将此图像路径在mysql中打印到客户端视图。但是我不知道该怎么办。请让我知道。
控制器
export const getBoardCreate = (req, res) =>
res.render("boardCreate", {
homeName: "watchingame",
pageName: "boardCreate",
routers
});
export const postBoardCreate = (req, res) => {
try {
let sql =
"insert into board(folder,title,content, nicName,writeDate,views,comments) values(?,?,?,1,now(),0,0)";
dbConnection.query(
sql,
[req.body.folder, req.body.title, req.body.content],
(err, result) => {
if (err) {
console.log(err);
} else {
const {
body: { folder, title, content },
file
} = req;
console.log(file);
}
}
);
res.redirect(`/board${routers.boardDetail(result.insertId)}`);
} catch (err) {
console.log(err);
}
};
视图
extends layouts/layout
block content
form(method="post" action=`/board${routers.boardCreate}` enctype="multipart/form-data")
select(name="folder")
option(value="select") select
option(value="wg") ~
option(value="news") ~
input(type="text" name="title" placeholder="title" required=true)
textarea(name="content" rows="20" cols="100%" required=true)
input(type="file" name="pictureFile" required=true accept="image/*")
input(type="submit" value="submit")
答案 0 :(得分:0)
使用multer npm软件包,它将文件存储在本地文件夹中并返回路径,并且您可以将路径存储在MySQL数据库中