[英]How to upload and save image to mongodb using node js
I am working on a project and i need to upload and save image to MongoDB.我正在处理一个项目,我需要将图像上传并保存到 MongoDB。 I using NodeJS as backend and Angular Material for frontend.我使用 NodeJS 作为后端,使用 Angular Material 作为前端。 I am writing the node and angular in TypeScript.我正在用 TypeScript 编写节点和角度。 How this upload and save can happen.这种上传和保存是如何发生的。 I want also to know how I can reed and display them.我也想知道我怎样才能芦苇和展示它们。
example of use使用示例
store.js
import multer from 'multer';
const storage = multer.diskStorage({
destination: function (req, file, cb) {
const path = process.cwd() + '\\uploads';
cb(null, path);
},
filename: function (req, file, cb) {
cb(null, `file-${Date.now()}.${file.originalname.split(/[.]+/).pop()}`);
}
});
export const UploadHandler = multer({
storage: storage,
fileFilter(req, file, callback, acceptFile) {
if (['image/png'].indexOf(file.mimetype) === -1) {
return callback(new Error("This File Is Not Supported"), false);
}
return callback(null, true);
}
});
app.js
import store from './store';
app.post('/upload', store.array('files', 1), function (req, res, next) {
if(req.files && req.files.length > 0) {
// files uploaded successfully
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.