[NestJS] Multiple FileUpload (feat.[object: null prototype])

Dragon_Tack·2023년 8월 31일

NestJS 프로젝트에서 파일 업로드 기능을 작업하다가 업로드 된 파일에 형식에 문제가 있는거 같아서 회고하고자 글을 작성했다.
[object: null prototype] <- 이거땜에 하루종일 고생해서 나중에 생각날때
단단해지려고 일단 쓰러왔다.

단수

  @UseInterceptors(FileInterceptor('file', option))
{
  fieldname: 'file',
  originalname: 'image.jpg',
  encoding: '7bit',
  mimetype: 'image/jpeg',
  buffer: <Buffer ... 848448 more bytes>,
  size: 1237787
}

복수

  @UseInterceptors(
    FileFieldsInterceptor(
      [
        { name: 'file_1', maxCount: 1 },
        { name: 'file_2', maxCount: 1 },
      ],
      option,
    ),
  )

{files}.controller.ts

  @Post('/upload')
  async uploadedHandler(
    @UploadedFiles(option)
    files: {
      file_1: Express.Multer.File;
      file_2: Express.Multer.File;
    },
  ) {
    return files
  }

[object: null prototype]

오늘의 주인공,,

[object: null prototype] : {[
  {
    fieldname: 'files',
    originalname: 'image.jpg',
    encoding: '7bit',
    mimetype: 'image/jpeg',
    buffer: <Buffer ... 1231232 more bytes>,
    size: 123128
  },
  {
    fieldname: 'files',
    originalname: 'knowledge_graph_logo.png',
    encoding: '7bit',
    mimetype: 'image/png',
    buffer: <Buffer ... 99982 more bytes>,
    size: 52434
  }
]}

[object: null prototype] 아까 위에도 언급했듯이,, 이런형태로 나오는게 정말 낯설기도하고 저거를 없애고 작업하는 방법은 없을까 ? 여러 고민이 많은 형태로,,,일단 이 글을 마친다.
일단 필자는 하위에 있는 내용으로 작업을 마저 진행했다. 나중에 더 좋은 방법이 있으면 공유 부탁해요.
files.file_1[0] , files.file_2[0] 의 형태로 사용중.

참고문헌 : https://docs.nestjs.com/techniques/file-upload

profile
고민의 흔적을 늘여놓는 공간

0개의 댓글