Angular 带多部分文件和其他参数的角度POST请求
我想从angular发出POST请求,而后端开发人员正在等待类似这样的spring启动请求,有3个参数:Angular 带多部分文件和其他参数的角度POST请求,angular,typescript,spring-boot,post,Angular,Typescript,Spring Boot,Post,我想从angular发出POST请求,而后端开发人员正在等待类似这样的spring启动请求,有3个参数: @PostMapping(value = "/import", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) public ResponseEntity<?> importFile( @RequestParam(value = "type") Dto dto, @Reques
@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 {
force出现错误:类型为“boolean”的参数不能分配给类型为“string | Blob”的参数。
那么,我如何设置3个参数来尊重后端呢
感谢您的帮助您正在尝试将布尔值传递到
formData.append
函数中。您应该将其转换为字符串
导入文件(fileToImport:fileToImport,booleanValue?:boolean){
const formData=new formData();
formData.append('type',fileToImport.type);
append('force',booleanValue.toString());
formData.append('file',fileToImport.file);
返回此.http.post('/import',formData);
}
如果后端特别关注布尔值的解析方式,则可能需要进行更多的处理。请记住,布尔值的Javascript字符串表示形式是
“true”
和“false”
什么是文件导入类型?很抱歉,我犯了一个错误。这是我无法添加到formData的布尔值
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);
}