[英]Image uploading platform using node-js, mongoose and multer
I'm desperately trying to build an image gallery in node-js where I can: 1. use a form to upload an image from the harddrive 2. save the uploaded file in mongo-DB (mongoose) 3. Retrieve the uploaded images and show it in my website 我正拼命尝试在node-js中建立图片库,我可以在其中进行以下操作:1.使用表单从硬盘驱动器上载图片2.将上载的文件保存在mongo-DB(猫鼬)中3.检索上载的图像并在我的网站上显示
I'm quite new to node and also mongodb, so I have a really hard time doing this (doing the same for simply news / text-only db-schemas was so easy). 我对node和mongodb还是很陌生,所以我很难做到这一点(对仅新闻/纯文本的db-schemas进行相同操作非常容易)。 I have searched all similar topics before, but none of them helped me, so I'm really desperate now since I had to copy some code-parts that I don't understand on the way to solving it.
我之前搜索过所有类似的主题,但是没有一个主题对我有帮助,所以我现在真的很绝望,因为我不得不复制一些我在解决它的方式中不了解的代码部分。 Please help me with some beginners explanations!
请帮我一些初学者的解释!
My html form 我的HTML表单
<form id="uploadForm" action="/images" method="POST" enctype="multipart/form-data">
<div class="form-group">
<label for="imageSelector">Select Image</label>
<input type="file" class="form-control" name="image" placeholder="upload image" id="imageSelector" accept="image/png, image/jpeg">
</div>
<div class="form-group imagePreview">
<p>No files currently selected for upload</p>
</div>
<div class="form-group">
<label for="imageDescription">Image Description</label>
<input type="text" class="form-control" name="description" placeholder="description" id="imageDescription">
</div>
<button class="btn btn-success">submit</button>
</form>
My DB-Schema: 我的数据库架构:
let imageSchema = new mongoose.Schema({
img: { data: Buffer, contentType: String },
description: String});
module.exports = mongoose.model("Image", imageSchema); //Exporting my Schema
My route file: 我的路线文件:
let express = require("express");
let mongoose = require("mongoose");
let bodyParser = require("body-parser");
let router = express.Router({mergeParams: true});
let multer = require("multer");
let Image = require("../models/image"); //Using DB-Schema that I exported
const GridFsStorage = require('multer-gridfs-storage');
const storage = new GridFsStorage({
url: "mongodb://localhost/My_DB",
file: (req, file) => {
return {
filename: 'file_' + Date.now()
};
}
});
const upload = multer({ storage });
const sUpload = upload.single('image');
router.post('/', sUpload, (req, res, next) => {
console.log("FILENAME: " + req.file.filename);
console.log("CHUNKSIZE: " + req.file.chunkSize);
console.log("CONTENTTYPE: " + req.file.contentType);
Image.create(req.file, function(err, addedImage) {
if(err) {
console.log(err);
} else {
console.log("SUCCESSFULLY ADDED NEW IMAGE! " + addedImage);
res.redirect("/images");
}
})
});
module.exports = router;
Now, what do I have to do to get the actual file to retrieve it? 现在,我该怎么做才能获取实际文件以进行检索? When I load my html-form and upload an image, after clicking submit, I get the following console.logs:
当我加载html表单并上传图像时,单击“提交”后,将获得以下console.logs:
FILENAME: file_1522841638818
CHUNKSIZE: 261120
CONTENTTYPE: image/png
PATH: undefined
ADDED NEW IMAGE SUCCESSFULLY! { _id: 5ac4b82674d2091a8db36f2f, __v: 0 }
When I go to my DB "My_DB" and take a look at db.images.find() the image is not uploaded. 当我进入数据库“ My_DB”并查看db.images.find()时,该图像未上传。 So I need to know 1. How to upload it into my DB?
所以我需要知道1.如何将其上传到数据库中? 2. How to retrieve it and really use the file itself to show it within html ?
2.如何检索它并真正使用文件本身在html中显示它?
Thanks very much! 非常感谢!
Btw.: I implemented this whole multer-gridfs-storage thing because I red that you can't upload images > 16 MB without it. 顺便说一句:我实现了整个multer-gridfs-storage的事情,因为我认为没有它,您将无法上传大于16 MB的图像。 I think I'm not really using multer yet so maybe you can give me some hints of how I can get to the image file.
我想我还没有真正使用multer,所以也许您可以给我一些有关如何获取图像文件的提示。 I know, you might talk about some docu of specific packages that I implemented.
我知道,您可能会谈论一些我实施的特定软件包的文档。 Believe me, this is only one attempt of about 100 different attempts to approach this and I'm really confused now of what is important for me :-/
相信我,这只是大约100种不同尝试中的一种,而现在我真的很困惑对我来说很重要的事情:-/
Use 采用
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, '/tmp/my-uploads')
},
filename: function (req, file, cb) {
cb(null, file.fieldname + '-' + Date.now())
}
})
var upload = multer({ storage: storage })
Url : is file system path not mongo path Also Your Model need data ,contenttype,description fields 网址:文件系统路径不是mongo路径您的模型也需要数据,内容类型,描述字段
img: {
data: Buffer,
contentType: String },
description: String});
But req.file does not contains it on same key so please create new json with key mapping 但是req.file不在同一密钥上包含它,因此请使用密钥映射创建新的json
More Info : https://github.com/expressjs/multer 更多信息: https : //github.com/expressjs/multer
要将图像存储到mongoDB,您需要在链接https://devdactic.com/ionic-image-upload-nodejs-server/中编写类似这样的服务
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.