I am learning how to upload files with nestjs. Specifically if my submission form has a title and an image file. I want the photo to be saved after I check some conditions. I have created an api to upload then use HttpService but it doesn't seem to work.
This is the upload controller:
@Post('/image')
@UseGuards(AdminAuthGuard)
@ApiOperation({ summary: 'Upload image Api ' })
@UseInterceptors(FileInterceptor('file', optionsImage))
async upload(@UploadedFile('file') file): Promise<{ filename: any }> {
return { filename: `/image/${file.filename}` };
}
I tried like this, but it doesn't work:
const formData = new FormData();
formData.append('file', file)
await this.http.post('http://localhost:3000/upload/image', formData)