Rakesh .p
Rakesh .p

Reputation: 79

Displaying binary docs to front end from marklogic database

Is it possible to bring a binary file from marklogic database to front end user by executing a node program of marklogic node js api?

Upvotes: 0

Views: 98

Answers (2)

You do not have a very clear question. Or at least not very specific. In general, binary documents are treated the same as any other content when queried and returned. There are some items in node.js that you may care about - like chunked data.

Please see here for general information: https://docs.marklogic.com/guide/app-dev/binaries

For what I think you are possibly asking, have a look at this great write-up on node.js and binary content: https://developer.marklogic.com/blog/working-with-binary-documents This page is full of goodies that may help you. I suggest that you run through the doc from top to bottom. It is worth the read. Specifically, have a look at the titled section "Displaying Images". This is for an image. However, the technique would be the same for any other binary content.

Upvotes: 1

Tamas
Tamas

Reputation: 11214

Yes it is possible, although some more information would be required from your end - what kind of binary are we talking about here, whether you'd like to serve it or make it available for download and if you use any Node.js package to serve this content (e.g. ExpressJS). Generally speaking it's good practice to use streams to read binaries in chunks as opposed to read the whole binary out from the database in one piece.

Here's a code snippet using Express and Node.js demonstrating this concept:

const displayImage = ((req, res) => {
  const uri = `/image/${req.params.id}`; //use your URI
  res.writeHead(200, { 'Content-type': 'image/png' }); //use your Content-type
  const data = [];
  db.documents.read(uri).stream('chunked')
  .on('data', chunk => data.push(chunk))
  .on('error', error => console.error(error))
  .on('end', () => {
    let buffer = new Buffer(data.length).fill(0);
    buffer = Buffer.concat(data);
    res.end(buffer);
  });
});

Upvotes: 2

Related Questions