1

Requirement: Working on image file upload. Here, using express and node.js. Received binary data in file using busboy package.

My question is how to receive binary data from file to local variable to insert in mongo db.

var binaryData = "";

var busboy = new Busboy({ headers: req.headers });
busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
  console.log('File [' + fieldname + ']: filename: ' + filename + ', encoding: ' + encoding + ', mimetype: ' + mimetype);
  file.pipe(/********want to receive binary data to binaryData************/); 
});

How to get binary data into binaryData variable?

Any alternative approach to receive file content to write directly on mongodb (not gridfs).

Thank you.

1 Answer 1

1

You can pipe to any writable stream or if you want to handle the data yourself (for buffering purposes or otherwise), you can use the 'data' and 'end' events or use file.read() and the 'readable' event to manually read data from the stream. The data in the file stream is typically binary, but you will have to check encoding and/or mimetype to be sure.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.