将图像上传到 mongoDB 并取回图像的 url:Multer
Upload image to mongoDB and get back the url of the image: Multer
我正在尝试将图像上传到 MongoDB,但是当我在 mongoCompass 中对其进行可视化时,它只显示其“_id”。所以不知道有没有通过
我需要:
- 将图像存储在数据库中
- 获取图像的 URL 存储在那里。
我正在使用 Express、Multer、Body-parser、fs、mongoose
这是我的 app.js
const express = require("express"),
app = express(),
bodyParser = require("body-parser"),
fs = require("fs"),
multer = require("multer"),
mongoose = require("mongoose");
mongoose.connect("mongodb://localhost/Images");
app.use(bodyParser.urlencoded(
{ extended:true }
))
app.set("view engine","ejs");
//Schema
var imgSchema = mongoose.Schema({
img:{data:Buffer,contentType: String,nom:String}
});
var image = mongoose.model("image",imgSchema);
// SET STORAGE
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, './uploads')
},
filename: function (req, file, cb) {
cb(null, 'uploaded-' + Date.now()+".png")
}
})
var upload = multer({ storage: storage })
app.get("/",(req,res)=>{
res.render("index");
})
app.get("/show",(req,res)=>{
image.find().toArray(function (err,result){
const imgArray = result.map(element =>element._id);
console.log(imgArray);
if(err){
return console.error(err);
}
res.send(imgArray)
})
});
app.post("/uploadphoto",upload.single('myImage'),(req,res)=>{
var img = fs.readFileSync(req.file.path);
var encode_img = img.toString('base64');
var final_img = {
contentType:req.file.mimetype,
image:new Buffer(encode_img,'base64'),
nom:"hi"
};
image.create(final_img,function(err,result){
if(err){
console.log(err);
}else{
console.log(result.img.Buffer);
console.log("Saved To database");
res.contentType(final_img.contentType);
res.send(final_img.image);
}
})
})
//Code to start server
app.listen(2000,function () {
console.log("Server Started at PORT 2000");
})
this is what mongo shows me
This is the image upload folder
提前致谢
一切都在这里
这个 Github 回购有详细的答案。
https://github.com/AnasGara/Upload-Image-Express-Mutler
我正在尝试将图像上传到 MongoDB,但是当我在 mongoCompass 中对其进行可视化时,它只显示其“_id”。所以不知道有没有通过
我需要:
- 将图像存储在数据库中
- 获取图像的 URL 存储在那里。
我正在使用 Express、Multer、Body-parser、fs、mongoose
这是我的 app.js
const express = require("express"),
app = express(),
bodyParser = require("body-parser"),
fs = require("fs"),
multer = require("multer"),
mongoose = require("mongoose");
mongoose.connect("mongodb://localhost/Images");
app.use(bodyParser.urlencoded(
{ extended:true }
))
app.set("view engine","ejs");
//Schema
var imgSchema = mongoose.Schema({
img:{data:Buffer,contentType: String,nom:String}
});
var image = mongoose.model("image",imgSchema);
// SET STORAGE
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, './uploads')
},
filename: function (req, file, cb) {
cb(null, 'uploaded-' + Date.now()+".png")
}
})
var upload = multer({ storage: storage })
app.get("/",(req,res)=>{
res.render("index");
})
app.get("/show",(req,res)=>{
image.find().toArray(function (err,result){
const imgArray = result.map(element =>element._id);
console.log(imgArray);
if(err){
return console.error(err);
}
res.send(imgArray)
})
});
app.post("/uploadphoto",upload.single('myImage'),(req,res)=>{
var img = fs.readFileSync(req.file.path);
var encode_img = img.toString('base64');
var final_img = {
contentType:req.file.mimetype,
image:new Buffer(encode_img,'base64'),
nom:"hi"
};
image.create(final_img,function(err,result){
if(err){
console.log(err);
}else{
console.log(result.img.Buffer);
console.log("Saved To database");
res.contentType(final_img.contentType);
res.send(final_img.image);
}
})
})
//Code to start server
app.listen(2000,function () {
console.log("Server Started at PORT 2000");
})
this is what mongo shows me
This is the image upload folder
提前致谢
一切都在这里 这个 Github 回购有详细的答案。 https://github.com/AnasGara/Upload-Image-Express-Mutler