在我的Angular项目中,我在上传之前使用了ngx-image-cropper
来裁剪图像.component.ts的一些相关部分包括:
croppedImage: any = '';
imageCropped(event: ImageCroppedEvent) {
this.croppedImage = event.base64;
}
Base64ToFile(url: any, filename: any, mimeType: any){
return (fetch(url)
.then(function(res){return res.arrayBuffer();})
.then(function(buf){return new File([buf], filename,{type:mimeType});})
);
}
OnSubmit() {
const filedata = new FormData();
this.Base64ToFile(this.croppedImage, "myphoto.png", "image/png").then(function(outputFile){
console.log(outputFile);
filedata.append("file", outputFile, outputFile.name);//The first parameter "file" is the input parameter in API method
});
this.http.post('https://localhost:5001/api/featuredpost/postimage/' + this.selectedPostId, filedata).subscribe(
response => {
console.log(response);
}
);
}
API action:
public async Task<IActionResult> PostImage([FromForm] IFormFile file, [FromRoute] int id){
...
}
问题是,当我调试API时,input file
参数为null,而cropped image
的字符串值为base64
.我该怎么修?