我想从angular发出POST请求,而后端开发人员正在等待带有3个参数的这样的spring boot请求:
@PostMapping(value = "/import", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<?> importFile(
@RequestParam(value = "type") Dto dto,
@RequestParam(value = "booleanValue", required = false) Boolean booleanValue,
@RequestParam("file") MultipartFile file)
throws IOException {
在Angular方面,我正在尝试构建表单数据,但是在编写此代码时,我无法添加布尔值:
importFile(fileToImport: FileToImport, booleanValue?: boolean) {
const formData = new FormData();
formData.append('type', fileToImport.type);
formData.append('booleanValue', booleanValue);
formData.append('file', fileToImport.file);
return this.http.post('/import', formData);
}
部队出了错:Argument of type 'boolean' is not assignable to parameter of type 'string | Blob'
所以我如何放置3个参数以尊重后端?
感谢您的帮助
formData.append
函数。您应该将其转换为字符串。importFile(fileToImport: FileToImport, booleanValue?: boolean) {
const formData = new FormData();
formData.append('type', fileToImport.type);
formData.append('force', booleanValue.toString());
formData.append('file', fileToImport.file);
return this.http.post('/import', formData);
}