2

Long story short, I used a DataForm object that contains a blob of an image as one of the parts and sent it to my node.js server into the formidable parser:

    //blob enters the files variable
    formParser.parse(req, (err, fields, files)=>{
        if(err)throw err; 


        const fileInfo = JSON.parse(fields.JSON);

        //figure out how to read the blob, how to get it's array buffer, then write to C disk
        console.log(files);

        //fs.writeFile() here




    })

How do I extract the Blobs ArrayBuffer from this persistent file in formidable that looks like this:

{
  Binary: PersistentFile {
    _events: [Object: null prototype] { error: [Function (anonymous)] },
    _eventsCount: 1,
    _maxListeners: undefined,
    lastModifiedDate: 2022-06-15T17:44:50.214Z,
    filepath: 'C:\\Users\\rnata\\AppData\\Local\\Temp\\f962bfe8ab505addfba1e4600',
    newFilename: 'f962bfe8ab505addfba1e4600',
    originalFilename: 'blob',
    mimetype: 'image/png, image/jpg, image/jpeg',
    hashAlgorithm: false,
    size: 373021,
    _writeStream: WriteStream {
      _writableState: [WritableState],
      _events: [Object: null prototype],
      _eventsCount: 1,
      _maxListeners: undefined,
      path: 'C:\\Users\\rnata\\AppData\\Local\\Temp\\f962bfe8ab505addfba1e4600',
      fd: 4,
      flags: 'w',
      mode: 438,
      start: undefined,
      autoClose: true,
      pos: undefined,
      bytesWritten: 373021,
      closed: false,
      [Symbol(kFs)]: [Object],
      [Symbol(kCapture)]: false,
      [Symbol(kIsPerformingIO)]: false
    },
    hash: null,
    [Symbol(kCapture)]: false
  }
}

What classes do I need, or what methods do I have to use, so that I can extract an arraybuffer and use it in fs.writeFile().

Thank you!

Rodion N.
  • 21
  • 5
  • I also want to know the answer – Sanjana Ekanayake Aug 08 '22 at 08:18
  • 3
    This is the answer. Formidable already writes the binary file by default automatically to the C:Drive if you don't specify a directory. So you have to find the file by the file name property, and then use fs.rename to rename it, then since it's already a binary file, you just use fs.readfile() on it to read it's binary data. To summarize, formidable auto saves the file to C: drive as a binary file already, then use fs.readFile() to get it's binary data. – Rodion N. Aug 09 '22 at 14:57

1 Answers1

1

I was having the same issue. In my case I was be able to fix it in that way:

  1. Get filepath in that PersistentFile object.
  2. import * as fs from "fs";
  3. const srcToFile = (src: string) => fs.readFileSync(src);

Send that filepath to srcToFile function. It is going to return to you a buffer/Object.

I was having hard time to upload my formdata's file to AWS S3. After parsing the multiform data and converting the files to Buffer, I got be able to upload it. I hope it works for you too.