How do I return a file-stream from buffer?
Asked Answered
A

1

7

I have stored my image, it's size in bytes and its type on a mysql db.

When I fetch it I am getting back a buffer for the image and now Im trying to figure out how to send it back to my client so that it renders the image?

Code inside of my route:

  const img = await db.images.findByPk(parser.setValueAsBIN(p.id));

   const myReadableStreamBuffer = new streamBuffers.ReadableStreamBuffer({
    frequency: 10, // in milliseconds.
    chunkSize: img.Length, // in bytes.
  });



 myReadableStreamBuffer.put(img.dataValues.imageData);

Whats the next step?

If I would to log myReadableStreamBuffer

I just get:

Readable {   _readableState:    ReadableState {
     objectMode: false,
     highWaterMark: 16384,
     buffer: BufferList { head: null, tail: null, length: 0 },
     length: 0,
     pipes: null,
     pipesCount: 0,
     flowing: null,
     ended: false,
     endEmitted: false,
     reading: false,
     sync: true,
     needReadable: false,
     emittedReadable: false,
     readableListening: false,
     resumeScheduled: false,
     paused: true,
     emitClose: true,
     autoDestroy: false,
     destroyed: false,
     defaultEncoding: 'utf8',
     awaitDrain: 0,
     readingMore: false,
     decoder: null,
     encoding: null },   readable: true,   domain: null,   _events: [Object: null prototype] {},   _eventsCount: 0,   _maxListeners: undefined,   stopped: false,   stop: [Function],   size: [Function],   maxSize: [Function],   put: [Function],   _read: [Function] }
Ashy answered 22/5, 2020 at 1:51 Comment(1)
Try converting Buffer data to base 64 string using buffer.toString(encodingType). That should be utilised in client side.Tallyho
E
15

Fastify support stream and buffer too in the reply.send() method.

Here how to manage them:


const fs = require('fs')
const { Readable } = require('stream')
const fastify = require('fastify')({ logger: true })

fastify.get('/', (req, reply) => {
  const buffer = fs.readFileSync('demo.png')
  reply.type('image/png') // if you don't set the content, the image would be downloaded by browser instead of viewed
  reply.send(buffer)
})

fastify.get('/stream', (req, reply) => {
  const buffer = fs.readFileSync('demo.png') // sync just for DEMO
  const myStream = new Readable({
    read () {
      this.push(buffer)
      this.push(null)
    }
  })

  reply.type('image/png')
  reply.send(myStream)
})

fastify.listen(3000)

(I would avoid stream-buffers package since it seems no more maintained - issues unanswered - and the default stream module in node.js has been greatly improved)

Engadine answered 22/5, 2020 at 6:50 Comment(4)
If I may bother you a little bit more with this. Your anser above works but Im curious how do I show the streamed image in the client? I set image sr attribute to "localhost:4000/cool/uploadHandler/…" and I the response type to reply.type('application/octet-stream'); But no image is shown.Ashy
the client is doing an ajax request? If so you could just add an <img src="http://localhost:4000/cool/uploadHandler/id=11ea9bc5-559b-1d33-a070-00ff98e8d5ab" /> to the DOMEngadine
It always amazes me when some of the best answers get no upvotes. I love that you have the whole deal and two perfect examples.Talion
Thanks. I wanted to read from and sql db, convert it to a CSV and stream it. Is it possible? ThanksPuccini

© 2022 - 2024 — McMap. All rights reserved.