Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upload different fields through FileFieldsFastifyInterceptor, first one is only processed #7

Open
MuAladdinIbrahim opened this issue Jan 19, 2024 · 2 comments

Comments

@MuAladdinIbrahim
Copy link

Hello mate,
Thank you for those interceptors, you saved me time.

I face a problem using FileFieldsFastifyInterceptor which is the interceptor process only the first file in the form-data, all other fields are ignored. when I swap the fields in the request, the first one still be processed despite anything else.

  @UseInterceptors(
    FileFieldsFastifyInterceptor(
      [
        { name: 'logo', maxCount: 1 },
        { name: 'copy', maxCount: 1 },
      ],
      imageUploadOptions,
    ),
  )
  @Put('/:id/info')
  updateInfo(
    @Param('id') id: string,
    @Body() updateInfoDTO: UpdateInfoDTO,
    @UploadedFiles()
    { logo, copy },
  ) {
    console.log({ logo, copy }); // always one of them is undefined
    updateInfoDTO.logo = logo && logo[0]?.path;
    copy =
      commercialRegistrationCopy && copy[0]?.path;
    return this.service.updateInfo(id, updateInfoDTO);
  }
export const imageUploadOptions = {
  storage: diskStorage({
    destination: './uploads/images',
    filename: editFileName,
  }),
  limits: {
    fileSize: +process.env.maxAvatartFileSize || 3 * 1000 * 1000, /// 3mb,
  },
  fileFilter: imageFileFilter,
};

can you help?

@chanphiromsok
Copy link
Owner

you can take a look at this example https://github.com/chanphiromsok/example-fastify-file-interceptor/blob/main/src/app.controller.ts sorry for the late response Do you have reproduced the repo?
now switch to Mobile Development I don't have much time I will try to fix if it is the issues

@chanphiromsok
Copy link
Owner

chanphiromsok commented Feb 19, 2024

Hello mate, Thank you for those interceptors, you saved me time.

I face a problem using FileFieldsFastifyInterceptor which is the interceptor process only the first file in the form-data, all other fields are ignored. when I swap the fields in the request, the first one still be processed despite anything else.

  @UseInterceptors(
    FileFieldsFastifyInterceptor(
      [
        { name: 'logo', maxCount: 1 },
        { name: 'copy', maxCount: 1 },
      ],
      imageUploadOptions,
    ),
  )
  @Put('/:id/info')
  updateInfo(
    @Param('id') id: string,
    @Body() updateInfoDTO: UpdateInfoDTO,
    @UploadedFiles()
    { logo, copy },
  ) {
    console.log({ logo, copy }); // always one of them is undefined
    updateInfoDTO.logo = logo && logo[0]?.path;
    copy =
      commercialRegistrationCopy && copy[0]?.path;
    return this.service.updateInfo(id, updateInfoDTO);
  }
export const imageUploadOptions = {
  storage: diskStorage({
    destination: './uploads/images',
    filename: editFileName,
  }),
  limits: {
    fileSize: +process.env.maxAvatartFileSize || 3 * 1000 * 1000, /// 3mb,
  },
  fileFilter: imageFileFilter,
};

can you help?

Do you have any error log ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants