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.69k stars 7.63k forks source link

path is undefined when using file FilesInterceptor for file uploading #13650

Closed jabedzaman closed 5 months ago

jabedzaman commented 5 months ago

Is there an existing issue for this?

Current behavior

I am trying to upload folders from a react frontend with rtk query and react dropzone. I am getting the path of the file on the frontend. On the backend I am using FilesInterceptor and UploadedFiles to get the files, but the file path becomes undefined. I tried to keep preservePath: true, but still same issue...

Frontend file upload code:

multipleFilesUpload: builder.mutation<
      MultipleFilesUploadResponse,
      MultipleFilesUploadRequest
    >({
      query: ({ formData }) => ({
        url: "/files/upload/multiple",
        method: "PUT",
        formData,
      }),
      invalidatesTags: ["Files", "Meta"],
      onQueryStarted: async (_, { dispatch, queryFulfilled }) => {
        const { formData } = _;
        console.log({ formData: formData.getAll("files") });
        toast.info(`Uploading ${formData.getAll("files").length} files...`);
        try {
          const { data, meta } = await queryFulfilled;
          console.log({ data, meta });
          dispatch(
            actions.removeFilesFromQueue({
              files: Array.from(formData.getAll("files") as File[]),
            })
          );
          toast.success("Files uploaded successfully");
        } catch (error) {
          toast.error("Failed to upload files");
        }
      },
    }),```

    My controller at the backend:
@Put('/multiple')

@UseInterceptors( FilesInterceptor('files', 100, { preservePath: true, }), ) putFiles( @AuthUser() user: IUser, @UploadedFiles() files: Array, ) { console.log({ gotFiles: files[0].path, // this is shown as undefined... }); // return this.filesService.uploadFiles(files, user.uuid); return { message: 'Files uploaded successfully', }; }


  tried with axios but still same issue 

  frontend logs:
  path here is retained
<img width="284" alt="image" src="https://github.com/nestjs/nest/assets/99767817/ba2a4aea-a607-4957-88e0-185ff21f8599">

on backend its lost
<img width="405" alt="image" src="https://github.com/nestjs/nest/assets/99767817/7f6d8aac-1b50-4f16-a1a0-92622755e805">

### Minimum reproduction code

https://github.com/jabedzaman/nest-error-reproduce-test

### Steps to reproduce

_No response_

### Expected behavior

It was expected that the path field should have the path which was posted from the frontend 

### Package

- [X] I don't know. Or some 3rd-party package
- [ ] <code>@nestjs/common</code>
- [ ] <code>@nestjs/core</code>
- [ ] <code>@nestjs/microservices</code>
- [ ] <code>@nestjs/platform-express</code>
- [ ] <code>@nestjs/platform-fastify</code>
- [ ] <code>@nestjs/platform-socket.io</code>
- [ ] <code>@nestjs/platform-ws</code>
- [ ] <code>@nestjs/testing</code>
- [ ] <code>@nestjs/websockets</code>
- [X] Other (see below)

### Other package

_No response_

### NestJS version

^10.0.0

### Packages versions

```json
"dependencies": {
    "@aws-sdk/client-s3": "^3.588.0",
    "@nestjs/common": "^10.0.0",
    "@nestjs/config": "^3.2.2",
    "@nestjs/core": "^10.0.0",
    "@nestjs/microservices": "^10.3.8",
    "@nestjs/mongoose": "^10.0.6",
    "@nestjs/platform-express": "^10.0.0",
    "@nestjs/swagger": "^7.3.1",
    "@nestjs/terminus": "^10.2.3",
    "@prisma/client": "5.14.0",
    "class-transformer": "^0.5.1",
    "class-validator": "^0.14.1",
    "compression": "^1.7.4",
    "helmet": "^7.1.0",
    "mime": "^4.0.3",
    "mongoose": "^8.4.1",
    "reflect-metadata": "^0.2.0",
    "rxjs": "^7.8.1",
    "sanitize-filename": "^1.6.3"
  },

Node.js version

v20.12.2

In which operating systems have you tested?

Other

No response

jabedzaman commented 5 months ago

on the picture tagged for backend the logged object should have the file path which was /je/images copy2.jpg on client

kamilmysliwiec commented 5 months ago

Thank you for taking the time to submit your report! From the looks of it, this could be better discussed on our Discord. If you haven't already, please join here and send a new post in the #⁠ 🐈 nestjs-help forum. Make sure to include a link to this issue, so you don't need to write it all again. We have a large community of helpful members, who will assist you in getting this to work.