I have this nestjs controller import { Controller, Post, UploadedFile, UseInterceptors,…
- User
I have this nestjs controller
import { Controller, Post, UploadedFile, UseInterceptors, } from '@nestjs/common'; import { FileInterceptor } from '@nestjs/platform-express'; import { CloudinaryService } from './cloudinary.service';
@Controller('images') export class CloudinaryController { constructor(private readonly cloudinaryService: CloudinaryService) {}
@Post() @UseInterceptors(FileInterceptor('image')) async uploadImage(@UploadedFile() file: Express.Multer.File) { const uploadStream: any = this.cloudinaryService.uploadImage(file);
return uploadStream;} }
And this service
import { Injectable } from '@nestjs/common'; import { UploadApiErrorResponse, UploadApiResponse, v2 } from 'cloudinary'; import toStream = require('buffer-to-stream');
@Injectable() export class CloudinaryService { async uploadImage( fileName: Express.Multer.File, ): Promise<UploadApiResponse | UploadApiErrorResponse> { return new Promise((resolve, reject) => { const upload = v2.uploader.upload_stream((error, result) => { if (error) return reject(error); resolve(result); }); toStream(fileName.buffer).pipe(upload); }); } }
How can I refactor so that I can update multiple files
- Assistant
To update multiple files, you can make the following changes to your code:
- Update the
uploadImagemethod in theCloudinaryServiceclass to accept an array of files:
async uploadImage( files: Express.Multer.File[], ): Promise<(UploadApiResponse | UploadApiErrorResponse)[]> { return Promise.all( files.map((file) => { return new Promise((resolve, reject) => { const upload = v2.uploader.upload_stream((error, result) => { if (error) return reject(error); resolve(result); }); toStream(file.buffer).pipe(upload); }); }), ); }- Update the
uploadImagemethod in theCloudinaryControllerclass to accept an array of files:
@Post() @UseInterceptors(FileInterceptor('image', { limits: { files: 10 } })) async uploadImages(@UploadedFiles() files: Express.Multer.File[]) { const uploadStreams: any[] = await this.cloudinaryService.uploadImage(files); return uploadStreams; } - Update the
- User
How can I test this new implementation in postman
- Assistant
To test the new implementation in Postman, you can follow these steps:
-
Start your NestJS server if it is not already running.
-
Open Postman and create a new POST request.
-
Set the request URL to the endpoint for the
CloudinaryControlleryou want to test. In your case, it should behttp://localhost:3000/imagesif you are running the server locally. -
Go to the "Body" tab in Postman and select "form-data".
-
Add a new key-value pair for each file you want to upload. Make sure the key matches the parameter name in your controller's
@UseInterceptors(FileInterceptor('image'))decorator. The value should be the file you want to upload. -
Click the "Send" button to make the request.
Postman will send the request to your server, which will handle the file uploads using the new implementation. The server will upload each file to Cloudinary and return the uploaded file objects as the response. You can then inspect the response in Postman.
-
- User
I get this response { "statusCode": 400, "message": "Unexpected field", "error": "Bad Request" }
- Assistant
The error message "Unexpected field" usually indicates that the server is expecting a different field name in the form-data request than what is provided. In your case, it seems that the field name
imageis not matching the expected field name in the controller'sFileInterceptordecorator.To resolve this issue, make sure the field name in the form-data request matches the one specified in the
FileInterceptordecorator. In this case, the field name should beimage. Here's how you can update your Postman request:-
Open the Postman request where you are testing the file upload.
-
Go to the "Body" tab. Instead of using the "form-data" option, choose "File" option.
-
Click on "Choose Files" and select the files you want to upload.
-
Make sure the key/name of the file input field is
image(should match the parameter name in your controller'sFileInterceptordecorator). -
Click the "Send" button to resend the request.
By ensuring that the field name in the form-data request matches the one specified in the decorator, you should be able to successfully upload multiple files using the updated implementation and get the expected response.
-