dmitriy-nz / nestjs-form-data

NestJS middleware for handling multipart/form-data, which is primarily used for uploading files.
https://www.npmjs.com/package/nestjs-form-data
MIT License
117 stars 23 forks source link

returning invalid array format when uploading multiple files #47

Closed Pangeran29 closed 11 months ago

Pangeran29 commented 1 year ago

hello.. i try to upload many image using nestjs-form-data but the array response is not valid array of objects, and i can't iterate it this is my dto configuration: @ApiProperty({ type: 'array', items: { type: 'string', format: 'binary', }, required: true, }) @IsNotEmpty() @IsFile({ each: true }) @MaxFileSize(2000000, { each: true }) @HasMimeType(['image/jpeg', 'image/png', 'image/jpg'], { each: true }) banner: FileSystemStoredFile[];

and when i try console.log it returning invalid array format [ originalName: 'picture_991_posterproduk-masarasara-pants-1-sz.png', encoding: '7bit', busBoyMimeType: 'image/png', path: 'D:\\nguli\\maxsol\\goon\\public\\banner\\picture_991_posterproduk-masarasara-pants-1-sz-87b8a7.png', size: 114098, fileType: { ext: 'png', mime: 'image/png' }, '': FileSystemStoredFile { originalName: 'Screenshot 2022-09-07 160046.png', encoding: '7bit', busBoyMimeType: 'image/png', path: 'D:\\nguli\\maxsol\\goon\\public\\banner\\Screenshot 2022-09-07 160046-7b8a72.png', size: 284564, fileType: { ext: 'png', mime: 'image/png' }, '': FileSystemStoredFile { originalName: 'Screenshot 2022-09-07 160046.png', encoding: '7bit', busBoyMimeType: 'image/png', path: 'D:\\nguli\\maxsol\\goon\\public\\banner\\Screenshot 2022-09-07 160046-b8a721.png', size: 284564, fileType: [Object] } } ]

is this the right response when uploading array of image in nestjs-form-data??

dmitriy-nz commented 1 year ago

Hi, show how you send a request, e.g. through a rest client such as postman or insomnia

Pangeran29 commented 1 year ago

@dmitriy-nz yes, i use swagger to send the request. this is how it looks in swagger image and this is the curl (banner is the array of image that i send) image

dmitriy-nz commented 1 year ago

@Pangeran29 Please note the readme, the section "Validation of file arrays" https://github.com/dmitriy-nz/nestjs-form-data#validate-the-array-of-file The key must be in the format "key[]". I don't quite understand what's wrong with your array output in the log, please make it readable

reislucaz commented 1 year ago

I have the same problem there. The format of array is like: [...data from file 1, ' ': [...data from file 2, ' ': [ ...data from file 3 ] ] ]

reislucaz commented 1 year ago

image

nikola418 commented 11 months ago

Any progress on this issue?

nikola418 commented 11 months ago

Here is a very dirty workaround: @Transform((obj) => { const arr = []; let field = obj.value; while (field) { const temp = field['']; delete field['']; arr.push(Object.assign(new FileSystemStoredFile(), field)); field = temp; } return arr; })

dmitriy-nz commented 11 months ago

@nikola418 Please read doc:

  1. You must use the IsFilesdecorator to validate an array of files. https://github.com/dmitriy-nz/nestjs-form-data#isfiles
  2. You must explicitly show arrays in your Multipart Form keys. https://github.com/dmitriy-nz/nestjs-form-data#validate-the-array-of-file.
    Actually, it should work and yes, I did some investigation and the append-field library used to parse field keys and build the multi level object is not working as it should. I don't have a lot of time right now, I'll get to it soon.
Yuniac commented 11 months ago

Here is a very dirty workaround: @Transform((obj) => { const arr = []; let field = obj.value; while (field) { const temp = field['']; delete field['']; arr.push(Object.assign(new FileSystemStoredFile(), field)); field = temp; } return arr; })

This was the only way to make it work for me. Thanks.

dmitriy-nz commented 11 months ago

I use this library in many projects, including for loading arrays of files, if you follow the readme. Anyway, I fixed a bug in the node-append-field library in version 1.9.1 and now everything should work fine without explicitly specifying the array sign in the file key. Open the issue again if the problem occurred after updating to version 1.9.1.

Altair200333 commented 5 months ago

Here is a very dirty workaround: @Transform((obj) => { const arr = []; let field = obj.value; while (field) { const temp = field['']; delete field['']; arr.push(Object.assign(new FileSystemStoredFile(), field)); field = temp; } return arr; })

i think this can be shortened to this, which also worked for me:

@Transform(({ obj }) => obj.files)
dmitriy-nz commented 2 months ago

This issue was completely fixed in the v1.9.9, please reopen issue if you still have that problem.