我试图通过将对象从Typescript(Angular 4)传递到web api进行处理来保存对象。我在对象中有一个图像。但是每次遇到应该连接到web api的 Create 服务时,它都会抛出400个错误请求。我已经在互联网上搜索了几天,但我发现不仅没有解决方案有效,而且不同之处在于,他们尝试单独上传文件而不是文件在对象中(Json对象)。以下是我到目前为止所尝试的内容。
在HTML表单中,我有一个文件控件,用它来选择要保存的图像
<input #imageInput type="file" class="form-control" (change)="onImageChange()" />
打字稿
@ViewChild('imageInput') imageInput;
onImageChange() {
let imgInput = this.imageInput.nativeElement;
if (imgInput.files && imgInput.files[0]) {
this.file = imgInput.files[0];
}
}
save(): void {
this.saving = true;
this.tax.branchId = this.appSession.user.branchId;
this.tax.scanImage = this.file;
console.log(this.file);
this._taxService.create(this.tax)
.finally(() => { this.saving = false; })
.subscribe(() => {
this.notify.success(this.l('SavedSuccessfully'), 'Tax Saved');
this.close();
this.modalSave.emit(null);
});
}
创建服务(TypeScript)
create(input: CreateTaxMapDto): Observable<TaxDto> {
let url_ = this.baseUrl + '/api/services/app/Tax/SaveTax';
const content_ = JSON.stringify(input ? input.toJS() : null);
return this.http.request(url_, {
body: content_,
method: 'post',
headers: new Headers({
'Content-Type': 'application/json; charset=UTF-8',
'Accept': 'application/json; charset=UTF-8'
})
});
}
content_
的位置:
{
"taxTypeId": "4",
"taxTypeName": null,
"branchId": null,
"branchName": null,
"dateOfReceipt": "2017-11-17",
"amount": 23423434,
"taxAuthorityId": "1",
"taxAuthorityName": null,
"remark": null,
"scanImage": {}
}
当它点击创建服务时,它会抛出400错误请求错误。错误的细节如下;
> {code: 0, message: "Your request is not valid!", details: "The
> following errors were detected during validation. ↵ - ↵",
> validationErrors: Array(1)} code : 0 details : "The following errors
> were detected during validation. ↵ - ↵" message : "Your request is
> not valid!" validationErrors : Array(1) 0 : members : ["scanImage"]
> message : ""
> __proto__ : Object length : 1
> __proto__ : Array(0)
> __proto__ : Object
从上面的错误中, ScanImage 就是问题所在。我该如何解决这个问题?