I'm not able to get a file from my Angular 11 application uploaded to my .NET 5 service. If I set a breakpoint at the first line of the controller method it's not hit. The angular app just gets an HttpErrorResponse
with a status of 0. I'm able to POST
without issue to other methods in this same controller, so I know all the "setup" type stuff is working properly.
I'm doing the upload like so on the Angular side:
postUploadLegacyData(files: File[]): Observable<string[]> {
const formData = new FormData()
files.forEach(x => formData.append('file', x, x.name))
return this.http.post<string[]>('requests/import', formData)
}
and then on the .NET side I have this:
[Route("[controller]"), Produces("application/json"), Authorize, ApiController]
public class RequestsController : ControllerBase
{
[HttpPost("import")]
[Consumes("multipart/form-data")]
public IActionResult PostImportExistingRequests(IEnumerable<IFormFile> files)
Resolved
I found that it should instead be this on the server side:
public async Task<IActionResult> PostImportExistingRequestsAsync()
{
var form = await Request.ReadFormAsync();
var files = form.Files;