Saturday, 27 May 2017

Multer returning File instead of an array

Im trying to upload multiple files to a nodeJS using the multer module. Reading the docs and req.files is suppose to be an array but I am getting an object with the field name as the key and a File object as the value.

Previously I had this with with angular2 but after switching over to react Im now having these issues. I am using the native fetch library which might make a difference but I don't think so since the request looks proper.

I have tried npm installing multer and event cloning multer from github into the node_modules directory

I am using Node v7.2, multer v1.2 with react on the frontend with native fetch to make the request.

Thanks


Node route:

const upload = multer({ dest: config.uploads.tempDir })
...
this.app.get('/uploads/:type/:filename', upload.fields([{ name: 'files[]', maxCount: 10 }]), this.display.bind(this));

The form data:

content-type:multipart/form-data; boundary=----WebKitFormBoundaryuWi9tvnulMlxRl1m

------WebKitFormBoundaryuWi9tvnulMlxRl1m

Content-Disposition: form-data; name="files"; filename="0fR0RmH.jpg"
Content-Type: image/jpeg

------WebKitFormBoundaryuWi9tvnulMlxRl1m
Content-Disposition: form-data; name="files"; filename="1d41a1d957c241d38a1f0782698710b2.jpeg"
Content-Type: image/jpeg

------WebKitFormBoundaryuWi9tvnulMlxRl1m--

req.files output

{ files: 
       File {
         domain: null,
         _events: {},
         _eventsCount: 0,
         _maxListeners: undefined,
         size: 713505,
         path: '/var/folders/85/dsxc639j6dg5yc6ljhy_vmhm0000gn/T/upload_8a839d16dc74cfd1709bdd78699b05c8',
         name: '1d41a1d957c241d38a1f0782698710b2.jpeg',
         type: 'image/jpeg',
         hash: null,
         lastModifiedDate: 2017-05-27T16:20:24.665Z,
         _writeStream: 
          WriteStream {
            _writableState: [Object],
            writable: false,
            domain: null,
            _events: {},
            _eventsCount: 0,
            _maxListeners: undefined,
            path: '/var/folders/85/dsxc639j6dg5yc6ljhy_vmhm0000gn/T/upload_8a839d16dc74cfd1709bdd78699b05c8',
            fd: null,
            flags: 'w',
            mode: 438,
            start: undefined,
            autoClose: true,
            pos: undefined,
            bytesWritten: 713505,
            closed: true } } }


The form:

<input 
    ref="fileUpload"
    name="files"
    multiple
    type="file"
    onChange={() => this.props.onChange(this.refs.fileUpload) }


via locrizak

No comments:

Post a Comment