gpt4 book ai didi

node.js - MongoDB:存储的base64缓冲区数据和检索的base64不匹配

转载 作者:太空宇宙 更新时间:2023-11-04 02:30:34 25 4
gpt4 key购买 nike

我使用 Mongoose 将小尺寸的 Base64 编码图像(每个大约 100-200K)直接发布到 mongodb 中。在存储之前,我可以看到内容与客户端发送的内容相同。但是查询的base64字符串和进去的不一样?知道我缺少什么吗?

    // process the add image request
app.post('/addimage', function(req, res) {
console.log("Addimage post....");
var qry = {'_email' : req.user.local.email, 'date' : req.body.date, 'subject' : req.body.subject };
Item.findOne(qry, function(err, item) {
if (err) {
console.log('Find images failed: ' + err);
var resp = '{"result" : "failed", "msg" : ' + err + ', "_req" : "addimage"}';
res.send(resp);
} else {
if (item == null) { // item doesn't exist, create now
var item = new Item();
item._email = req.body._email;
item.date = req.body.date;
item.subject = req.body.subject;
}

// add image
var image = new Image();
image.timestamp = req.body.timestamp;
image.contentType = req.body.contentType;
var imgBuf = new Buffer(req.body.image, 'base64');
image.image = imgBuf;
item.images.push(image);

item.save(function(err, result) {
if (err) {
console.log('Find images failed: ' + err);
var resp = '{"result" : "failed", "msg" : ' + err + ', "_req" : "addimage"}';
res.send(resp);
} else {
res.send('{"result" : "ok", _req: "addimage"}');
console.log("Image saved and responded to ...");
}
});
}
});
});

存储在 req.body.image 中的数据字符串以“/9j/4AAQSkZJ ...”之类的内容开头,但从 mongo shell 中我看到它不同。是否是 Bindata 的子类型 0 导致了这种情况?

> db.items.find({subject:"Science"},{'images.image':1})

{ "_id": ObjectId("547382943fc884447a767d58"), "图像": [ { "图像": BinData(0,"LwA5AGoALwA0AEEAQQBRAFMAawBaAEoAUgBnAEEAQgBBAFEAQQBBAEEAUQBBAEIAQQBBAEQALwA0AFEAQgBZAFIAWABoAHAAW gBnAEEAQQBUAFUAMABBAEsAZwBBAEEAQQBBAGcAQQBBAGcARQBTAEEAQQBN

架构:

var imageSchema = new mongoose.Schema({
timestamp : String,
contentType : String,
image : Buffer,
}, { strict: true });
imageSchema.index({ timestamp: 1}, { unique: true });

var itemSchema = new mongoose.Schema({
_email : {
type: String,
required: true,
},
date : {
type: String,
required: true,
},
subject : {
type: String,
required: true,
},
images : [imageSchema],
}, { strict: false });
itemSchema.index({ _email: 1, date: 1, subject: 1}, { unique: true });

Mongodb 版本 2.6.5、Mongoose 3.8.1、Node v0.11.6-pre

此外,在创建 Buffer 对象时尝试使用“utf8”/“ascii”/“ucs2”而不是“base64”。

谢谢!

最佳答案

这是 Node 版本;(恢复到0.10.33解决了问题!

关于node.js - MongoDB:存储的base64缓冲区数据和检索的base64不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27112588/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com