How to upload file with additional form data
See original GitHub issueI’m submitting a…
[ ] Regression
[ ] Bug report
[ ] Feature request
[ X ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.
I’m using nestjs/swagger
and trying to figure out a way to upload a file and also pass additional form data along with the image.
I’m using this to upload the image, and it works fine.
@UseInterceptors(FileInterceptor('file'))
@ApiConsumes('multipart/form-data')
@ApiImplicitFile({ name: 'file', required: true })
uploadFile(@UploadedFile() file) {
console.log('got upload');
console.log('...file', file);
}
However, I’d like to include some meta data about my file as form data. I tried using @Body()
, but that expects a json payload, and the upload requires multipart/form-data.
@UseInterceptors(FileInterceptor('file'))
@ApiConsumes('multipart/form-data')
@ApiImplicitFile({ name: 'file', required: true })
uploadFile(@UploadedFile() file, @Body() body: MyDto) {
console.log('got upload');
console.log('...file', file);
console.log('...body', body);
}
I also tried ApiImplicitBody
, but couldn’t seem to get it to work. I’m just guessing because I seem to be deep in undocumented territory.
What is the correct way to:
- Access other properties of the multi-part form (beyond the file)
- Define that as part of the swagger definition with the swagger decorators
Issue Analytics
- State:
- Created 5 years ago
- Comments:16 (2 by maintainers)
Top Results From Across the Web
How to upload single or multiple files the easy way with ...
We'll see examples of using FormData with Ajax, Angular 7, Ionic and React. ... Next, create the index.js file and add following code:...
Read more >Using FormData Objects - Web APIs | MDN
The FormData object lets you compile a set of key/value pairs to send using XMLHttpRequest . It is primarily intended for use in...
Read more >How to Multipart File Upload Using FormData with HTML | refine
In this guide, we will look at how we can upload a file from HTML form data to a server with the multipart-upload...
Read more >How to use FormData for AJAX file upload? - Stack Overflow
var formData = new FormData($('#upload_form')[0]); formData.append('tax_file', $('input[type=file]')[0].files ...
Read more >How to upload files with FormData using JavaScript
A quick guide to learn how to upload single as well as multiple files with FormData using JavaScript.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I experience a similar problem. The file upload does work for me using multipart/form-data, and I am successfully sending additional properties with it:
All additional properties are passed to the
modelData
when sending a proper POST request from the application. The issue is that swagger does not understand it and the “Try it out” does not work. It sends just the file without any additional data.i have been reading on @nestjs/swagger version 3.1.0 and it no support additional form data, if you want genarate document for this you need to write a custom decorator like this.
api-implicit-form-data.decorator.ts
and using it ad controller function like this: test.controller.ts
i tested on my project, and it working fine hope it help.