I am trying to code a program in nodejs that stores a list of files in mongodb. It works OK, but there is a problem: it always stores the contentType metadata as binary/octet-stream, and I would like it to store the actual mime-type. I have tried getting the mime type before the readStream (via promises), but even if I hardcode the contentType (for example "image/jpeg"), it always saves the metadata as "binary/octet-stream.
It is my code:
files.forEach(function(f) {
var conn = mongoose.createConnection(db);
conn.once('open', function () {
var gfs = Grid(conn.db);
var writeStream = gfs.createWriteStream({
filename: f.location,
mode: 'w',
contentType: 'image/jpeg'
});
writeStream.on('finish', function() {
console.log('Storing', f.location, 'OK');
return;
})
.on('error', function(data) {
console.log('Error', f.location, data)
});
fs.createReadStream(path.join(__dirname, 'files', f.location), 'utf-8')
.pipe(writeStream);
});
});
Any idea? Thank you for your responses!
In order to set the content type you need to do this:
var writeStream = gfs.createWriteStream({
filename: f.location,
mode: 'w',
content_type: 'image/jpeg'
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With