Search code examples
javascriptnode.jsfile-uploadbuffermulter

What is file buffer in Node.js?


I have seen in Multer (file uploading package for node.js) that I can store the file as usual and instead, I can have a buffer.

The buffer looks like this when console log:

<Buffer ff d8 ff e2 0b f8 49 43 43 5f 50 52 4f 46 49 4c 45 00 01 01 00 00 0b e8 00 00 00 00 02 00 00 00 6d 6e 74 72 52 47 42 20 58 59 5a 20 07 d9 00 03 00 1b ... >

What is this? How can it be used? Is this the image that was uploaded? If then - how to display this? Should it be saved in the DB and then.. what? Should I create a file from it and save the file and the actual reason for this is to not upload a real file until all validations are okay?


Solution

  • Buffers are just array of bytes (printed in hexadeciamal here 00 to ff, or 0 to 255.

    You can represent any file as a byte array, actually fs.writeFile can takes a Buffer directly.

    You can convert it to string.

    Check it out on the official documentation https://nodejs.org/api/buffer.html