Не могу загрузить файл с загрузчиком ngx

Я хочу загрузить файл с помощью ngx-uploader. Я не знаю, как установить данные, которые я хочу отправить в бэкэнд

backend.php

  public function add_icon() {
    $retval = array();
    $icon = '';
    $FILE_DIR = './images/';
    if($_POST || $_FILES) {
        if(!empty($_FILES['icon']['name'])) {
            $icon = $_FILES['icon']['name'];
        }
        $should_save = true;
        $guid = rand();
        if($icon != '') {
            $type       = strtolower($_FILES['icon']['name']);
            $extArray   = array(".jpg",".jpeg",".gif",".png");
            if (!in_array(strtolower(strrchr($type, ".")), $extArray)) {
                $retval['message'] = 'Invalid file format.';
                $should_save = false;
            }
            if($should_save) {
                $ext=$this->getExtension($_FILES['icon']['name']);
                $icon = time().'icon'.$guid.'.'.$ext;
                $icon = $this->uploadFile($_FILES['icon'], $FILE_DIR, $icon);
            }               
        }
        $retval['status'] = '1';
        $retval['message'] = 'file uploaded';
        $retval['filename'] = $icon;
    } else {
        $retval['status'] = '0';
        $retval['message'] = 'nothing was posted';
    }

    header('Content-Type: application/json');
    echo json_encode($retval, JSON_UNESCAPED_UNICODE);
    }

Код сервера будет работать, если я загружу файл через почтальона.

fileupload.ts

    onUploadOutput(output: UploadOutput): void {
    console.log(output);
    if (output.type === 'allAddedToQueue') {

    const event: UploadInput = {
    type: 'uploadAll',
    url: 'http://localhost/ezyrental/index.php/Ws/add_icon',
    method: 'POST',
    data: { foo: 'bar' }
    };
    this.uploadInput.emit(event);
   } else if (
    output.type === 'addedToQueue' &&
    typeof output.file !== 'undefined'
   ) {
    // add file to array when added
    this.files.push(output.file);
  } else if (
    output.type === 'uploading' &&
    typeof output.file !== 'undefined'
   ) {
   // update current data in files array for uploading file
    const index = this.files.findIndex(
    file => typeof output.file !== 'undefined' && file.id === output.file.id
   );
    this.files[index] = output.file;
   } else if (output.type === 'removed') {
   // remove file from array when removed
   this.files = this.files.filter(
     (file: UploadFile) => file !== output.file
   );
   } else if (output.type === 'dragOver') {
    this.dragOver = true;
   } else if (output.type === 'dragOut') {
   this.dragOver = false;
   } else if (output.type === 'drop') {
   this.dragOver = false;
   }
 }

Передняя часть вернула сообщение об успешном завершении, но файлы не были загружены, я думаю, что проблема заключается в отправке данных в неправильном формате, возможно, поэтому веб-сервис работает через почтальон. Как отформатировать данные с помощью ngxuploader

0 ответов

Другие вопросы по тегам