我正在尝试从我的数据库中读取一个文件,并且由于某种原因它每次都返回相同的文件而不考虑文件的名称,这是我试图获取它的方式。
回复:
router.post('/', (req, res) => {
mongoose
.connect('mongodb://dbname.mlab.com:12345/db', {
auth:
{
user: 'user',
password: 'pw'
}
})
.then(() => {
console.log('connection successful')
var gridfs = require('mongoose-gridfs')({
collection:'fs',
model:'files',
mongooseConnection: mongoose.connection
});
Attachment = gridfs.model;
// large file example
var stream = Attachment.readByFileName(req.body.imageName);
console.log(stream)
stream.on('data', (image) => {
base64data += image.toString('base64'); // converting buffer to base64 string
base64data = base64data.replace('undefined', '');
console.log(base64data)
});
stream.on('close', () => {
res.send(base64data)
});
})
.catch((err) => console.error(err));
})
FRONT:
getImageBuffer = (imageName) => {
this.forceUpdate();
console.log('this is the image im fatching', imageName)
axios
.post('https://myserver.com/readCarImg', {
imageName: imageName
})
.then((res) => {
this.setState({
imageBuffer:`data:image/jpg;base64,${res.data}`
})
})
.catch((e) => {
console.log('there was problem getting image buffer', e)
})
}
从状态中删除图像:
showModalMarker = () => {
this.setState({
imageBuffer: undefined
})
}
注意:我正在记录imageName
,以确保我每次都得到不同的名字,而我是。而且我在每次新调用之前从状态中删除Image字符串以确保它清理。我正在从服务器记录响应,并且可以确认每次都返回相同的缓冲区。我正在使用gridfs
将图像存储在我的mongo db中,如果它有所不同