Search code examples
javascriptnode.jsimagefileimagemagick-convert

How to convert image buffer to file object in NodeJS?


Purpose

How do I make a file in the same path in NodeJS in the same form as client request form?

// test.js 
const readFile = fs.readFileSync(filepath, (err, data) => {
if (err) {
    console.log(err)
} else {
    return data
} })
// result : <Buffer 89 50 4e 47 0d 0a 1a 0a ..... >
mysdk(readFile)
// error : it need to file name, size(bytes) , type ...

I want File object like this.

enter image description here

File locate

enter image description here


Solution

  • I solved... 😅

    const fs = require('fs')
    const path = require('path')
    const filepath = path.join(__dirname, './success.png')
    const image = fs.createReadStream(filepath)
    // now image converted like file object...