我正在开发一个在Node.js中上传/下载图片的功能。目前已经有一个POST请求,将图像以二进制形式保存在MongoDB中,并且还有一个GET请求,用于将该图像返回。但是我不知道如何使用该响应,因为它是一个数字数组,不知道如何转换。
以下是Mongo模型:
image.js const mongoose = require('mongoose'); const Schema = mongoose.Schema;
const ImageItem = new Schema({
id: {
type: String
},
value: {
type: Buffer
},
});
module.exports = Image = mongoose.model('image', ImageItem);
POST图片将在数据库中创建一个条目:
const image = require('../models/image');
const user = require('../models/user');
const multer = require('multer');
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, './uploads/');
},
filename: function (req, file, cb) {
cb(null, file.originalname + new Date().toISOString());
},
});
const upload = multer({
storage: storage,
});
module.exports = function (app) {
app.post('/upload', upload.single('value'), (req, res, next) => {
const newImage = new image({
id: req.body.id,
value: req.file.path,
});
newImage
.save()
.then((result) => {
console.log(result);
res.status(201).json({
message: 'created succesfully',
});
})
.catch((err) => {
console.log(err);
res.status(500).json({
error: err,
});
});
});
};
并在数据库中创建了一个条目:
为了获取图像,我创建了一个GET请求:
const image = require('../models/image');
module.exports = function (app) {
app.get('/upload/:id', (req, res) => {
console.log('req.body.id', req.params);
image.find({ id: req.params.id }, function (err, results) {
if (err) {
res.send(`error: ${err}`);
} else {
res.send(results);
}
});
});
};
这个在 Postman 中测试的内容返回一个包含数字数组的 JSON:
[
{
"_id": "5ebd1c112892f4230d2d4ab4",
"id": "email123@test.com",
"value": {
"type": "Buffer",
"data": [
117,
112,
108,
111,
97,
100,
115,
47,
117,
115,
101,
114,
80,
105,
99,
116,
117,
114,
101,
46,
112,
110,
103,
50,
48,
50,
48,
45,
48,
53,
45,
49,
52,
84,
49,
48,
58,
50,
51,
58,
49,
51,
46,
57,
51,
52,
90
]
},
"__v": 0
}
]
我该如何使用这些数据来获取实际图像?
const image
第一行应该放在哪里? - Leo MessiimageBuffer
以避免冲突。正如第二个代码片段所示,它被放置在路由上。 - Marcos Casagrande代理错误:无法将请求 /upload/email122345@test.com 从 localhost:3000 代理到 http://localhost:5000 (ECONNRESET)
。我正在使用nodemon从客户端和服务器运行我的应用程序。在尝试获取图像后,服务器崩溃:TypeError [ERR_INVALID_ARG_TYPE]:第一个参数必须是字符串类型或Buffer、ArrayBuffer或Array的实例或类似数组的对象。接收到未定义的值
。 - Leo Messi代理错误:无法将请求 /upload/email122345@test.com 从 localhost:3000 代理到 http://localhost:5000。 有关更多信息,请参见 https://nodejs.org/api/errors.html#errors_common_system_errors(ECONNRESET)。
我在服务器上使用5000,在客户端上使用3000。 - Leo MessiBUffer.from
。 - Marcos CasagrandeBuffer.from
的值是您显示的数组:[117, 112, 108]
,从您发布的 JSON 中是.value.data
,但在您的端上可能是不同的值。因此,只需传递实际的数组即可使其正常工作。 - Marcos Casagrande