question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Swagger upload file

See original GitHub issue

I’m submitting a…


[ ] Regression 
[x] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

After update to 4.x version, stops working uploading files in swagger ui

Expected behavior

File uploaded

Minimal reproduction of the problem with instructions

https://github.com/gulaandrij/nest-monorepo-swagger

  1. yarn
  2. nest start
  3. go to http://localhost:3000/api
  4. try to upload

What is the motivation / use case for changing the behavior?

Environment


Nest version: 6.7.2

 
For Tooling issues:
OS Version     : Linux 5.3
NodeJS Version : v13.1.0
YARN Version    : 1.19.2

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

9reactions
nartccommented, Dec 7, 2019

Please double check the latest documentations on File Upload

Here’s a quick fix:

  @Post('upload')
  @ApiConsumes('multipart/form-data')
  @ApiBody({
    schema: {
      type: 'object',
      properties: {
        file: {
          type: 'string',
          format: 'binary',
        },
      },
    },
  })
  @UseInterceptors(FileInterceptor('file'))
  uploadFile(@UploadedFile('file') file) {
    console.log(file);
  }

You can always create a custom decorator like this:

import { ApiBody } from '@nestjs/swagger';

export const ApiFile = (fileName: string = 'file'): MethodDecorator => (
  target: any,
  propertyKey: string,
  descriptor: PropertyDescriptor,
) => {
  ApiBody({
    schema: {
      type: 'object',
      properties: {
        [fileName]: {
          type: 'string',
          format: 'binary',
        },
      },
    },
  })(target, propertyKey, descriptor);
};

then use it:

  @Post('upload')
  @ApiConsumes('multipart/form-data')
  @ApiFile()
  @UseInterceptors(FileInterceptor('file'))
  uploadFile(@UploadedFile('file') file) {
    console.log(file);
  }

If your FileInterceptor() has a different fieldName like FileInterceptor('avatar') then adjust like so:

  @Post('upload')
  @ApiConsumes('multipart/form-data')
  @ApiFile('avatar')
  @UseInterceptors(FileInterceptor('avatar'))
  uploadFile(@UploadedFile() file) {
    console.log(file);
  }

Make sure to adjust your UploadedFile if you submit a FormData with many different values + file.

1reaction
goorscommented, Apr 20, 2020

I figured this one out after a long time:

ApiBody({
        type: 'multipart/form-data', /// this is redundant.
})

since you already have

@ApiConsumes("multipart/form-data")

Swagger generated code is ok. But Swagger UI will not work since multiple file upload is not supported yet.

So, in a fact you don’t really need seperate decorator you can just use

export class FileUploadDto {
  @ApiProperty({ type: "string", format: "binary" })
  file: any;
}

and then in your TS client you will just have new FormData() and then append files.

Both of these decorators generate Typescript code with function(model: Blob).

What a waste of time!!!

By far these @nestjs guys are so slow on to respond. They could saved me 5 days!

Read more comments on GitHub >

github_iconTop Results From Across the Web

File Upload - Swagger
File Upload. In OpenAPI 3.0, you can describe files uploaded directly with the request content and files uploaded with multipart requests. Use the...
Read more >
How to post files in Swagger (OpenAPI)? - Stack Overflow
and now I see the file upload option on my Swagger UI page. But when I select a file and click "try it...
Read more >
File Upload via Swagger - codeburst
The code snippet below shows an example action method which allows two files be uploaded at the same time. The Swagger UI then...
Read more >
Testing file upload with Swagger in ASP.Net core - Medium
Testing file upload with Swagger in ASP.Net core. preview image. Let's assume you have an API which is used for uploading image to...
Read more >
Upload File to Swagger UI OpenAPI Specification - YouTube
Add file uploading in Swagger UI using OpenAPI Specification V3.0 using IFormFile inbuilt support for Upload.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found