node-formidable / formidable

The most used, flexible, fast and streaming parser for multipart form data. Supports uploading to serverless environments, AWS S3, Azure, GCP or the filesystem. Used in production.
MIT License
7k stars 680 forks source link

Getting Fields out of json #811

Closed gaurishhs closed 2 years ago

gaurishhs commented 2 years ago

Support plan

Context

What are you trying to achieve or the steps to reproduce?

app.post('/upload', async (req, res) => {
    const form = new formidable.IncomingForm()
    form.parse(req, (error: any, fields: any, files: any) => {
        if(error) throw new Error(error)
        console.log(files)
        // imageDataURI.encodeFromFile(files.filepath).then((res: any) => console.log(res)).catch((e: any) => console.log(e))
    })
})

What was the result you got?

{
  '': PersistentFile {
    _events: [Object: null prototype] { error: [Function (anonymous)] },
    _eventsCount: 1,
    _maxListeners: undefined,
    lastModifiedDate: 2022-01-07T08:18:45.682Z,
    filepath: '/var/folders/sw/g7tk44p50dl3n_kgss8y6bwr0000gn/T/351263a04127c47c7bf9c2800',
    newFilename: '351263a04127c47c7bf9c2800',
    originalFilename: 'Screenshot_2022-01-01_at_10.36.58_AM-removebg-preview.png',
    mimetype: 'image/png',
    hashAlgorithm: false,
    size: 51200,
    _writeStream: WriteStream {
      fd: 25,
      path: '/var/folders/sw/g7tk44p50dl3n_kgss8y6bwr0000gn/T/351263a04127c47c7bf9c2800',
      flags: 'w',
      mode: 438,
      start: undefined,
      pos: undefined,
      bytesWritten: 51200,
      closed: false,
      _writableState: [WritableState],
      _events: [Object: null prototype],
      _eventsCount: 1,
      _maxListeners: undefined,
      [Symbol(kFs)]: [Object],
      [Symbol(kIsPerformingIO)]: false,
      [Symbol(kCapture)]: false
    },
    hash: null,
    [Symbol(kCapture)]: false
  }
}

What result did you expect?

GrosSacASac commented 2 years ago

The <input type="file"> should have a name property like <input type="file" name="something">

you well then be able to access it like so console.log(files.something?.filepath)

Also you are miss-using the word JSON

tunnckoCore commented 2 years ago

@GrosSacASac true.

But definitely we should check against such case. :rocket: :ship:

GrosSacASac commented 2 years ago

Check what ?

tunnckoCore commented 2 years ago

Ah, probably nothing.