Angular 6 - загрузка файлов неподдерживаемого типа мультимедиа 415 - PullRequest
0 голосов
/ 14 ноября 2018

Я протестировал мой API загрузки на Postman, он работает, но в моем приложении (Angular 6 + .net-core 2.1) сеть всегда возвращает ошибку 415 неподдерживаемого типа мультимедиа, поскольку Content-Type в заголовках запроса всегда application/JSON. В основном, я использую httpClient, чтобы вернуть FormData в Backend в IFormFile вместе с 2 параметрами в конечной точке. У меня был такой же код в другом приложении с Angular 5 + .net-core 2.0, и он работает просто отлично. Поэтому я понятия не имею, есть ли какие-либо серьезные изменения в Angular 6. У кого-нибудь есть предложения?

API Сервис:

  uploadIcoFile(icoGuid: any, category: any, file: any) {
    const headers = new HttpHeaders({ 'Content-Type': 'multipart/form-data' });
    return this.httpClient.post<any>(this.apiUrl + `ico/upload-file/${icoGuid}/${category}`, file, { headers: headers })
      .pipe(
        catchError(this.handleError)
      );
  }

Загрузить компонент:

  upload(event: any) {
    const fileBrowser = this.myInputVariable.nativeElement;
    if (fileBrowser.files) {
      if (fileBrowser.files[0].size < 10000) {    // 16000
        this.sharedFunctionsService.showWarningToast('File is too small, needs to be at least 10KB');
      } else {
        const formData: FormData = new FormData();
        formData.append('file', fileBrowser.files[0]);
        this._uploadIcoFile(this.icoId, this.type, formData);
      }
    }
  }

  _uploadIcoFile(icoGuid: any, category: any, file: FormData) {
    this.loading = true;
    this.apiService.uploadIcoFile(icoGuid, category, file).subscribe(data => {
      this.loading = false;
      this.fileUploaded.emit(true);
      this.reset();
      this.sharedFunctionsService.showSuccessToast('File successfully uploaded');
    },
      (error) => {
        this.loading = false;
        this.sharedFunctionsService.showErrorToast('An error happened when trying to upload file');
      });
  }

Контроллер:

[HttpPost("upload-file/{icoGuid}/{category}")]
[Authorize]
[Authorize(Policy = "Active")]
[ProducesResponseType(typeof(int), 200)]
public async Task<IActionResult> UploadIcoAttachment(Guid icoGuid, string category, IFormFile file)
{
  return Ok(await Mediator.Send(new UploadIcoAttachmentsCommand() {
    File = file,
    FileCategory = (FileCategory)Enum.Parse(typeof(FileCategory), category),
    AccountId = GetAccountId(),
    IcoGuid = icoGuid
  }));
}
...