onSuccessItem из ng2-file-upload не запускается и сервер получает пустой файл - PullRequest
0 голосов
/ 10 ноября 2019

У меня проблемы с загрузкой ng2-файла. Я пытаюсь проанализировать ответ от обратного вызова onSuccessItem в JSON в переменной типа фотографии, чтобы я мог отправить ответ API.

Как бы то ни было, обратный вызов не запускается, и конечная точка получает нулевое значение[fromdata] какая дата является единственным параметром, который обновляется.

Вот мой код.

       initializeUploader() {
        this.uploader = new FileUploader({
  url:
    this.baseUrl +
    'users/' +
    this.authService.decodedToken.nameid +
    '/photos', // we need the user id
  authToken: 'Bearer ' + localStorage.getItem('token'), 
  disableMultipart: true, 
  formatDataFunctionIsAsync: true,
  formatDataFunction: async item => {
    return new Promise((resolve, reject) => {
      resolve({
        name: item._file.name,
        length: item._file.size,
        contentType: item._file.type,
        date: new Date()
      });
    });
  },
  allowedMimeType: ['image/jpeg', 'images', 'png', 'jpg'],
  maxFileSize: 10 * 1024 * 1024, 
  autoUpload: false, 
  removeAfterUpload: true 
  });

this.hasBaseDropZoneOver = false;

this.response = '';

this.uploader.response.subscribe(res => (this.response = res));



this.uploader.onAfterAddingFile = file => {
  file.withCredentials = false;
};

this.uploader.onWhenAddingFileFailed = fileNotSupported => {
  this.alertify.error('The file you are trying to upload is not an 
      image');
};

this.uploader.onSuccessItem = (item: FileItem, resp, status, headers) => {
  if(resp){
    const res: Photo = JSON.parse(resp); 

    const photo = {
      id: res.id,
      url: res.url,
      dateadded: res.dateadded,
      description: res.description,
      ismain: res.ismain
    };

    this.photos.push(photo);
  }
  this.alertify.success('photo uploaded');
};
 }

А это фото интерфейс в Angular.

export interface Photo {

id: number;
url: string;
dateadded: Date;
ismain: boolean;
description: string;
}

И Dto, что конечная точка ожидает открытый класс PhotoForCreationDto {public string Url {get;установлен;}

      public IFormFile File { get; set; }
      public string Description { get; set; }
      public DateTime DateAdded { get; set; }
       // This is coming back rom cloudinary
       public string PublicPhotoId { get; set; }


      public PhotoForCreationDto()
      {
        DateAdded = DateTime.UtcNow;
      }
     }
...