我知道已经有一个类似的问题,但它没有解决上述错误。
这是我在我的 API 控制器中的 PUT 操作,并且在以下环境中运行良好Swagger:
[HttpPut("{id}/upload")]
public async Task<IActionResult> UploadFile(string id, IFormFile file)
{
// codes using id above are omitted, nothing to do with the error
var path = Path.Combine(
Directory.GetCurrentDirectory(),
"wwwroot",
file.FileName
);
using (var stream = new FileStream(path, FileMode.Create))
{
await file.CopyToAsync(stream);
}
return Ok();
}
以下是我的 Angular 网页中的代码:
HTML
<input type="file" (change)="setFile($event.target.files)"/>
<button (click)="save()"> Save </button>
打字稿
forUpload: File = null;
@Input() someModel: SomeModel;
// setting of file
setFile(myFile: File){
if (myFile === null){
return;
}
this.forUpload = myFile[0];
}
// saving
save() {
const addFileFnc = this.theService.addFile.bind(this.theService);
addFileFnc(this.someModel, this.forUpload).subscribe(
() => { this.ref.close(); },
(e) => { console.log(e); }
);
}
而且,我的服务是:
addFile(someModel: SomeModel, myFile: File): Observable<any> {
return this.http.put(`${api_url}/api/somemodels/${someModel.id}/upload`, {
id: someModel.id,
file: myFile
});
}
我也曾经FormData尝试过上述问题的答案,我将我的forUpload文件附加到一个FormData但不幸的是,同样的错误。如果我在媒体类型中没有任何类型的设置,我该如何解决这个问题?