【发布时间】:2021-03-06 00:51:30
【问题描述】:
我在使用 FormData 解析从前端发出的请求时遇到一些问题。这是 Postman 为 node.js Axios 生成的示例请求。如果我在请求中使用邮递员应用程序,它会按预期工作。
从 Postman 代码功能生成的前端示例。
var axios = require('axios');
var FormData = require('form-data');
var fs = require('fs');
var data = new FormData();
data.append('file', fs.createReadStream('/some_file.jpg')); **//I am using Electron and I have acces to the FileSystem from the client.**
data.append('resizeLargeImage[width]', '1920');
data.append('resizeLargeImage[height]', '1080');
data.append('resizeLargeImage[type]', 'cover');
var config = {
method: 'post',
url: 'localhost:3030/api/v1/optimize-single',
headers: {
'x-api-key': '123',
...data.getHeaders()
},
data : data
};
axios(config)
.then(function (response) {
console.log(JSON.stringify(response.data));
})
.catch(function (error) {
console.log(error);
});
后台
@Post('/optimize-single')
@UseInterceptors(FileInterceptor('file'))
async uploadFile(
@UploadedFile() file: FileDto,
@Body() body: UploadFileParametersDto,
@Res() response: Response,
): Promise<any> {
console.log('file', file, 'body', body);
**//File is undefined, body is a null Object**
return await this.appService.uploadFile(file, body, response);
}
关于为什么 Nest 无法识别此类请求的任何想法?
谢谢!
【问题讨论】:
标签: javascript typescript nestjs