问题描述
我的流程和代码:
从表单数据上传图像并将图像的BSON字符串存储到数据库中,以下是步骤和代码:
Uploading image from Form Data and storing image's BSON string to Database, below are the steps and code:
- 从multer上传中上传文件
- 从文件系统中读取文件-fs
- 转换图像内容字符串转换为base64字符串
- 将base64字符串转换为BSON字符串
- Uploading file from multer upload
- Reading file from filesystem - fs
- Converting image content string to base64 string
- Converting base64 string to BSON String
配置:
node -v: v12.13.1
npm -v: 6.12.1
fs-extra: ^8.1.0
multer: ^1.4.2
代码:
var upload = multer({
storage: multer.diskStorage({
destination: function (req, file, cb) {
cb(null, 'uploads')
},
filename: function (req, file, cb) {
cb(null, file.fieldname + '-' + Date.now())
}
})
});
upload.single('picture'), (req, res) => {
let imageString = fs.readFileSync(req.file.path);
let encodeImage = imageString.toString('base64');
let bufferImage = Buffer.from(encodeImage, 'base64');
var finalObj = {
contentType: req.file.mimetype,
image: bufferImage
};
db.collection('filesUpload').insertOne(finalObj, (err, result) => {
if (err) {
console.log(err);
}else{
console.log('success');
}
});
});
正在做什么?
我可以成功上传16MB以下的图像,并且可以从数据库正确读取和检索它。
What is working?I can able to upload below 16MB images successful and can read and retrieve it properly from Database.
我无法上传16MB以上的图像。
I can not able to upload above 16MB images.
RangeError [ERR_OUT_OF_RANGE]: The value of "offset" is out of range. It must be >= 0 && <= 17825792. Received 18646861
at Buffer.write (buffer.js:1019:5)
at serializeObjectId (D:\api\node_modules\mongodb\node_modules\bson\lib\bson\parser\serializer.js:274:14) at serializeInto (D:\api\node_modules\mongodb\node_modules\bson\lib\bson\parser\serializer.js:935:17)
at serializeObject (D:\api\node_modules\mongodb\node_modules\bson\lib\bson\parser\serializer.js:347:18)
at serializeInto (D:\api\node_modules\mongodb\node_modules\bson\lib\bson\parser\serializer.js:727:17)
at serializeObject (D:\api\node_modules\mongodb\node_modules\bson\lib\bson\parser\serializer.js:347:18)
at serializeInto (D:\api\node_modules\mongodb\node_modules\bson\lib\bson\parser\serializer.js:941:17)
at BSON.serialize (D:\api\node_modules\mongodb\node_modules\bson\lib\bson\bson.js:64:28)
at Msg.serializeBson (D:\api\node_modules\mongodb\lib\core\connection\msg.js:126:22)
at Msg.makeDocumentSegment (D:\api\node_modules\mongodb\lib\core\connection\msg.js:118:33)
at Msg.toBin (D:\api\node_modules\mongodb\lib\core\connection\msg.js:104:25)
at serializeCommand (D:\api\node_modules\mongodb\lib\core\connection\pool.js:779:41)
at Pool.write (D:\api\node_modules\mongodb\lib\core\connection\pool.js:927:3)
at _command (D:\api\node_modules\mongodb\lib\core\wireprotocol\command.js:128:10)
at command (D:\api\node_modules\mongodb\lib\core\wireprotocol\command.js:28:5)
at writeCommand (D:\api\node_modules\mongodb\lib\core\wireprotocol\write_command.js:47:3) {
code: 'ERR_OUT_OF_RANGE'
}
我不知道如何解决这个问题,我认为它是MongoDB错误,感谢您的帮助。
I am not getting how to solve this, i think its MongoDB error, thanks for your help.
推荐答案
在这里我已经使用过二进制JSON (称为 BSON文档),因此根据:
Here i have used Binary JSON called BSON Document, So there are limits to store in MongoDB as per MongoDB Docs:
- 最大BSON文档大小为16 MB
- MongoDB支持BSON文档嵌套的级别不超过100个
其他替换方法是什么?
- 定义:
- Definition:
根据MongoDB文档:
As per MongoDB documentation MongoDB GridFS Docs:
GridFS是用于存储和检索超过16 MB BSON文档大小限制的文件的规范。 GridFS不支持多文档事务。 GridFS不会将文件存储在单个文档中,而是将文件分为多个部分或大块,并将每个大块存储为单独的文档。
GridFS is a specification for storing and retrieving files that exceed the BSON-document size limit of 16 MB. GridFS does not support multi-document transactions. Instead of storing a file in a single document, GridFS divides the file into parts, or chunks, and stores each chunk as a separate document.
- API参考:
- API Reference:
如果您需要实现这是NodeJS,则MongoDB提供了Docs,这是
If you need to implement this is NodeJS then MongoDB has provided Docs, Here is the MongoDB API Reference Docs
- 堆栈溢出参考:
- Stack Overflow Reference:
如果需要实现方面的帮助,请参考堆栈溢出文章,此处是
If you need any help in implementation then refer stack overflow post, Here is the Stack Overflow Reference
这篇关于如何在MongoDB中设置缓冲区偏移范围,不允许在BSON Object中上传超过16MB的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!