我有一个处理上传文件请求的api (在spring boot中)如下:
@PostMapping("/api/admin/product/{id}/upload")
public ResponseEntity<Product> postUpload(@PathVariable("id") Integer id, @RequestParam("image") MultipartFile imageFile) {
Product product = productService.findOne(id);
return new ResponseEntity<>(productService.upload(product, imageFile),HttpStatus.OK);
}
以下是一个处理我的请求的服务。我应该如何修复它以适应上述 API?
@Injectable()
export class ProductServiceService {
private baseUrl = 'http://localhost:8080/api/admin/product';
private headers = new Headers({'Content-Type' : 'multipart/form-data', 'Access-Control-Allow-Origin' : '*'});
private options = new RequestOptions({headers: this.headers});
constructor(private _http: Http) { }
postUpload(id) {
const formdata: FormData = new FormData();
formdata.append('image', image);
return this._http.post(this.baseUrl + '/' + id + '/upload', formdata, this.options)
.map((res: Response) => res.json())
.catch(this.errorHandler);
}
errorHandler(error: Response) {
return Observable.throw(error || 'SERVER ERROR');
}
}
And this is the error after build
![enter image description here](https://istack.dev59.com/yYF4E.webp)