如何使用角度2+发送到服务器(文件和表单)

时间:2018-03-06 15:38:48

标签: php angular post

我正在尝试向php后端发送文件和表单数据,但我并不是想成功。

是否有人提示或甚至将发送部分修复到后端

HTML img

  <div class='form-group is-empty col-md-6'>
    <input style="display: none" type="file" (change)="onFileChanged($event)" #fileInput formControlName='arquivo_cifra'> 
    <div class="file-cifra" [style.background-image]="'url(' + img + ' )' | safeHtml: 'style' "></div>
    <button (click)="fileInput.click()" style="width: 250px">Select File</button>
  </div>
    <div class='box-footer'>
      <button (click)='save(form.value)' [disabled]='!form.valid' class='btn btn-primary'>Salvar</button>
    </div>

component.ts

  selectedFile: File;
  img: any = 'assets/img/user_photo.png';    
  onFileChanged(event) {
    var tmppath = URL.createObjectURL(event.target.files[0]);
    this.img = tmppath;
    this.selectedFile = event.target.files[0]; 
  }
  save(form) {
    const uploadData = new FormData();
    uploadData.append('myFile', this.selectedFile, this.selectedFile.name);
    uploadData.append('form', form); //formBuilder
    // this.form.value.arquivo_cifra = this.selectedFile;
    // this.cifraService.saveFile(uploadData);
    this.cifraService.save(uploadData)
  }

service.ts

  save(form) {
    return this.http.post(`${BE_API}/cifra.php?acao=salvar`, form)
      .subscribe((data) => {
        if (data['success'] == 'ok') {
          this.notify('Registro inserido com sucesso!');
          this.router.navigate(['/cifra'])
        }
        console.log(data);
      }, (error) => {
        this.notify(`Error: ${error}`);
      });
  }

后端php

文件是其工作但不是表单构建器。

 public function incluir($ar) {

        var_dump($_FILES);
        var_dump($_REQUEST);
        $dados = $this->dados($ar);
        die();
        $cd = self::insert($this->table, $dados);
        if($cd>0){
            self::commit();
        }        
    }

返回控制台

enter image description here

0 个答案:

没有答案