Send Images from MongoDB

I have been trying to send image from mongodb but nothing seems working
const imagePath = "path/to/the/image.jpeg";
const imageData = fs.readFileSync(imagePath, 'utf8');
const imageBuffer = Buffer.from(imageData);
const imageBinary = new mongodb.Binary(imageBuffer);
const imageMimeType = mime.lookup(imagePath);

image = {
name: i,
contentType: imageMimeType,
content: imageBinary
}
imagesData.push(image);
const imagePath = "path/to/the/image.jpeg";
const imageData = fs.readFileSync(imagePath, 'utf8');
const imageBuffer = Buffer.from(imageData);
const imageBinary = new mongodb.Binary(imageBuffer);
const imageMimeType = mime.lookup(imagePath);

image = {
name: i,
contentType: imageMimeType,
content: imageBinary
}
imagesData.push(image);
I did something like this to upload the image from my local directory
{
name: "shadow.jpg",
contentType: "image/jpeg",
content: "Binary.createFromBase64("base64 string comes here")"
}
{
name: "shadow.jpg",
contentType: "image/jpeg",
content: "Binary.createFromBase64("base64 string comes here")"
}
this is what it looks in the database
const image = await getImageContent('shadow', 'shadow.jpg');
const imagedata = Buffer.from(image.content.buffer);
const attachment = new AttachmentBuilder(imagedata, { name: 'shadow.jpg' });
await interaction.reply({files: [attachment] });
const image = await getImageContent('shadow', 'shadow.jpg');
const imagedata = Buffer.from(image.content.buffer);
const attachment = new AttachmentBuilder(imagedata, { name: 'shadow.jpg' });
await interaction.reply({files: [attachment] });
this is how I tried retriving it it sends a file...but the file is not containing any image
2 Replies
d.js toolkit
d.js toolkit7mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button! - Marked as resolved by OP
KruzVault
KruzVaultOP7mo ago
const imagePath = "path/to/the/image.jpeg";
const imageData = fs.readFileSync(imagePath);
const imageBuffer = Buffer.from(imageData);
const imageBinary = new mongodb.Binary(imageBuffer);
const imageMimeType = mime.lookup(imagePath);

image = {
name: i,
contentType: imageMimeType,
content: imageBinary
}
imagesData.push(image);
const imagePath = "path/to/the/image.jpeg";
const imageData = fs.readFileSync(imagePath);
const imageBuffer = Buffer.from(imageData);
const imageBinary = new mongodb.Binary(imageBuffer);
const imageMimeType = mime.lookup(imagePath);

image = {
name: i,
contentType: imageMimeType,
content: imageBinary
}
imagesData.push(image);
it should have been like this no need to read it with utf8 removing that fixed my code
Want results from more Discord servers?
Add your server