nestjs / nest

A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript 🚀
https://nestjs.com
MIT License
67.63k stars 7.62k forks source link

uplod more file in FileFieldsInterceptor only return one file #12904

Closed SGAMERyu closed 11 months ago

SGAMERyu commented 11 months ago

Is there an existing issue for this?

Current behavior

image To upload multiple files (all with different field name keys) but nest only return one file other key file is return emptry string image

Minimum reproduction code

https://codesandbox.io/p/devbox/upbeat-sea-rtxtwx?file=%2Fsrc%2Fapp.controller.ts%3A12%2C29

Steps to reproduce

No response

Expected behavior

return all files

Package

Other package

No response

NestJS version

10.0.0

Packages versions

"dependencies": {
    "@nestjs/common": "^10.0.0",
    "@nestjs/core": "^10.0.0",
    "@nestjs/platform-express": "^10.0.0",
    "@prisma/client": "^5.6.0",
    "cloudinary": "^1.41.0",
    "dotenv": "^16.3.1",
    "reflect-metadata": "^0.1.13",
    "rxjs": "^7.8.1"
  },
  "devDependencies": {
    "@nestjs/cli": "^10.0.0",
    "@nestjs/schematics": "^10.0.0",
    "@nestjs/testing": "^10.0.0",
    "@types/express": "^4.17.17",
    "@types/jest": "^29.5.2",
    "@types/multer": "^1.4.11",
    "@types/node": "^20.3.1",
    "@types/supertest": "^2.0.12",
    "@typescript-eslint/eslint-plugin": "^6.0.0",
    "@typescript-eslint/parser": "^6.0.0",
    "eslint": "^8.42.0",
    "eslint-config-prettier": "^9.0.0",
    "eslint-plugin-prettier": "^5.0.0",
    "jest": "^29.5.0",
    "prettier": "^3.0.0",
    "prisma": "^5.6.0",
    "source-map-support": "^0.5.21",
    "supertest": "^6.3.3",
    "ts-jest": "^29.1.0",
    "ts-loader": "^9.4.3",
    "ts-node": "^10.9.1",
    "tsconfig-paths": "^4.2.0",
    "typescript": "^5.1.3"
  },

Node.js version

v21.2.0

In which operating systems have you tested?

Other

No response

jmcdo29 commented 11 months ago

This worked fine when I ran it on my machine. I updated the controller to send back the properties as well, just to show how it's being read:

@Controller()
export class AppController {
  constructor(private readonly appService: AppService) {}

  @Post('add')
  @UseInterceptors(
    FileFieldsInterceptor([
      { name: 'poster', maxCount: 1 },
      { name: 'icon', maxCount: 1 },
    ]),
  )
  async add(@UploadedFiles() files, @Body() body) {
    console.log(files);
    console.log(body);
    return {
      files: Object.keys(files).map((file: any) => ({
        fieldName: files[file][0].fieldname,
        originalName: files[file][0].originalname,
      })),
      body,
    };
  }
}

Then made this request:

curl http://localhost:3000/add -F 'icon=@README.md' -F 'poster=@README.md' -F 'name=test'

And got this response

HTTP/1.1 201 Created
Connection: keep-alive
Content-Length: 132
Content-Type: application/json; charset=utf-8
Date: Fri, 08 Dec 2023 16:53:14 GMT
Etag: W/"84-w7KuOQTWKSnYt3rEgEUqrCw83s4"
Keep-Alive: timeout=5
X-Powered-By: Express

{
    "files": [
        {
            "fieldName": "icon",
            "originalName": "README.md"
        },
        {
            "fieldName": "poster",
            "originalName": "README.md"
        }
    ],
    "body": {
        "name": "test"
    }
}

Everything looks fine here