I have created below API. it is perfectly working from postman.
@Controller('certificates')
export class CertificatesController {
constructor(private readonly certificatesService: CertificatesService) {}
@Post('upload')
@UseInterceptors(
FileInterceptor('attachedFile', {
storage: diskStorage({
destination: './uploads',
filename(_, file, callback) {
const randomName = Array(32)
.fill(null)
.map(() => Math.round(Math.random() * 16).toString(16))
.join('');
return callback(null, `${randomName}${extname(file.originalname)}`);
},
}),
})
)
uploadFile(@UploadedFile() file) {
console.log(file);
return {
url: `http://localhost:8000/api/uploads/${file.filename}`,
};
}
}
but when I want to call the same using formik my API recive undefined in file.
<input
id="file"
name="attachedFile"
type="file"
onChange={(event: any) => {
setFieldValue(
'file',
event.currentTarget.files[0]
);}} />
const handleSubmit = async (values: any, httpClient: any, config: any) => {
alert(values.attachedFile); //it prints Object filelist
const { data } = await axios.post(
'http://localhost:3000/api/certificates/upload',
values.attachedFile
);
my API return below error and in UI I get 500.
why I am not able to send file along with post request? should I do something else? Other than file in another request I am able to send all the data. can you please help.