I am having some issues while attempting to upload a basic file to my backend dotnet core application. The problem is that the file ends up being null, whereas the description is not.
This is my .NET Core controller:
[AllowAnonymous]
[HttpPost]
[Route("upload")]
public async Task<IActionResult> ImageUpload(IFormFile file, [FromQuery] string description)
{
if (file == null)
{
return BadRequest("Failed to upload file");
}
// The file always turns out to be null!
return Ok("Good!");
}
And here is an excerpt from my Angular code for uploading the file:
async upload(file: File, description: string = '') {
if (file) {
const formData = new FormData();
formData.append('file', file, file.name);
return await this.http.post(route('image', 'upload'), formData, {
headers: {
'Content-Type': 'multipart/form-data'
},
params: { description },
responseType: "text"
}).toPromise();
} else {
return Promise.resolve('');
}
}