使用Node.js/Express和Mongoose将图像存储在MongoDB中

编程入门 行业动态 更新时间:2024-10-24 01:51:32
本文介绍了使用Node.js/Express和Mongoose将图像存储在MongoDB中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

当前,我使用angular-file-upload处理图像上传,我只是将图像保存到服务器的文件系统中,并在HTML中引用它.但是,我想尝试将图像直接存储在为博客文章定义的Schema中的数据库中.

Currently I handle image uploads using angular-file-upload and I simply save the image to the server's file system and reference it in the HTML. However, I want to try and store the image directly in the database within the Schema I defined for my blog posts.

var blogSchema = new Schema({ title: String, author: String, body: String, likes: { type: Number, default: 0 }, comments: [{ type: Schema.Types.ObjectId, ref: 'Comment' }], date: { type: Date, default: Date.now }, imageURL: String // instead of this image: // store it directly }); "imageURL: String" stores the path to the image.

我想这样做,这样我就可以拥有一个存储图像本身的字段.我当时在想,也许可以像以前一样上传图像,但是在上传图像之后将其转换并以二进制(或其他形式)存储在Mongo中.这可能吗?

I want to make it so that I can just have a field that stores the image itself. I was thinking that I could perhaps just upload the image like I already do, but instead convert the image after it has been uploaded and store it in binary (or some other form) in Mongo. Is this possible?

谢谢!

推荐答案

下面的示例显示了如何使用猫鼬将图像上传到MongoDB.单击此链接以获取原始来源

This example below shows how to upload an image to MongoDB using mongoose. Click this link for the original source

var express = require('express'); var fs = require('fs'); var mongoose = require('mongoose'); var Schema = mongoose.Schema; var imgPath = '/path/yourimage.png'; mongoose.connect('localhost', 'testing_storeImg'); var schema = new Schema({ img: { data: Buffer, contentType: String } }); var A = mongoose.model('A', schema); mongoose.connection.on('open', function () { console.error('mongo is open'); A.remove(function (err) { if (err) throw err; console.error('removed old docs'); // store an img in binary in mongo var a = new A; a.img.data = fs.readFileSync(imgPath); a.img.contentType = 'image/png'; a.save(function (err, a) { if (err) throw err; console.error('saved img to mongo'); // start a demo server var server = express.createServer(); server.get('/', function (req, res, next) { A.findById(a, function (err, doc) { if (err) return next(err); res.contentType(doc.img.contentType); res.send(doc.img.data); }); }); server.on('close', function () { console.error('dropping db'); mongoose.connection.db.dropDatabase(function () { console.error('closing db connection'); mongoose.connection.close(); }); }); server.listen(3333, function (err) { var address = server.address(); console.error('server listening on %s:%d', address.address, address.port); console.error('press CTRL+C to exit'); }); process.on('SIGINT', function () { server.close(); }); }); }); });

更多推荐

使用Node.js/Express和Mongoose将图像存储在MongoDB中

本文发布于:2023-11-22 19:17:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1618675.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图像   js   Node   Express   MongoDB

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!