Although I found a similar query, it didn't provide a solution to the error I am encountering.
Below is the PUT action in my API's Controller, which functions correctly in Swagger
:
[HttpPut("{id}/upload")]
public async Task<IActionResult> UploadFile(string id, IFormFile file)
{
// code snippets using id are excluded, unrelated to 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();
}
Here are the snippets of code in my Angular webpage:
HTML
<input type="file" (change)="setFile($event.target.files)"/>
<button (click)="save()"> Save </button>
TypeScript
forUpload: File = null;
@Input() someModel: SomeModel;
// file setup
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); }
);
}
My service counterpart is as follows:
addFile(someModel: SomeModel, myFile: File): Observable<any> {
return this.http.put(`${api_url}/api/somemodels/${someModel.id}/upload`, {
id: someModel.id,
file: myFile
});
}
Even after attempting to use FormData
as suggested in the aforementioned post, where I added my forUpload
File to a FormData
, I am still facing the same error. Is there a way to resolve this issue without any specific media type configurations?