Как я могу загрузить изображения на свой контроллер Laravel из Ionic 4? - PullRequest
0 голосов
/ 27 сентября 2019

У меня есть форма эха в ionci 4, которую я хочу отправить на мой контроллер, а затем сохранить в базе данных mysql.Форма работает правильно, пока вы не отправляете изображения.Попробуйте добавить headers.append ('enctype', 'multipart / form-data');но это не работает .. это мой код ts

SendDataFu(){

 var headers = new Headers();
 headers.append("Accept", 'application/json');
 headers.append('Content-Type', 'application/json' );
let postData =  {
    name: this.usuario.name,
    email: this.usuario.email,
    password: this.usuario.password,
    emp_telprivado: this.usuario.emp_telprivado,
    emp_nom: this.empresa.emp_nom,
    emp_foto: this.empresa.emp_foto,
    emp_ci: this.empresa.emp_ci,
    emp_direccion: this.empresa.emp_direccion,
    emp_telefono: this.empresa.emp_telefono,
    emp_whatsapp: this.empresa.emp_whatsapp,
    emp_info: this.empresa.emp_info,
    categoria_cat_id: this.empresa.categoria_cat_id,
    ciudad_ciu_id: this.empresa.ciudad_ciu_id,
    emp_tags: this.adicional.emp_tags,
    emp_instagram: this.adicional.emp_instagram,
    emp_twitter: this.adicional.emp_twitter,
    emp_web: this.adicional.emp_web,
    emp_facebook: this.adicional.emp_facebook,
    img1: this.adicional.img1,
    img2: this.adicional.img2,
    img3: this.adicional.img3,
    img4: this.adicional.img4,
    img5: this.adicional.img5,
    img6: this.adicional.img6,
    img7: this.adicional.img7,
    img8: this.adicional.img8,
    img9: this.adicional.img9,
    img10: this.adicional.img10,
    emp_latitud: this.adicional.emp_latitud,
    emp_longitud: this.adicional.emp_longitud,
    planes_id: this.plan,

}

 this.http.post("http://127.0.0.1:8000/api/user/registro/create", postData,{observe: 'response'})
   .subscribe(data => {
     console.log(data);

    }, error => {
     console.log(error);
   });

}

Как я уже сказал ... это всегда работает, когда я не отправляю изображения Мой контроллер

    public function store(EmpresaFormRequest $request) {
        //Datos Basicos de registro
        $user=new User;
        $user->name=$request->get('name');
        $user->email=$request->get('email');
        $user->password=Hash::make($request->get('password'));
        $user->nivel=3;
        $user->estado=0;
        $user->save();
        //Creacion de la empresa
        //Datos Generales
        $empresa=new Empresa;
        $empresa->emp_nom=$request->get('emp_nom');
        $empresa->emp_ci=$request->get('emp_ci');
        if(Input::hasFile('emp_foto')) {
            $file=Input::file('emp_foto');
            $control=0;
            $nombre = $file->getClientOriginalName();
            while ($control == 0) {
                if (is_file( public_path().'/imagenes/perfil/'.$nombre )) {
                    $nombre = rand() . $nombre;
                }else{
                    $file->move(public_path().'/imagenes/perfil/',$nombre);
                    $empresa->emp_foto=$nombre;
                    $control=1;
                }
            }
        }else{
            $empresa->emp_foto='default-avatar.png';
        }
        $empresa->emp_info=$request->get('emp_info');
        $empresa->emp_telprivado=$request->get('emp_telprivado');
        $empresa->emp_telefono=$request->get('emp_telefono');
        $empresa->emp_whatsapp=$request->get('emp_whatsapp');
        $empresa->emp_tags=$request->get('emp_tags');
        $empresa->categoria_cat_id=$request->get('categoria_cat_id');
        $empresa->ciudad_ciu_id=$request->get('ciudad_ciu_id');
        $empresa->planes_id=$request->get('planes_id');
        $empresa->users_id = $user->id;
        $empresa->vendedor_ven_id=1;
        $vencimiento = Carbon::now();
        $empresa->emp_vencimiento=$vencimiento;
        $empresa->emp_visto=0;
        $empresa->emp_activo=0;
        $empresa->pagado=0;
        //guardado de imagenes segun plan
        if(Input::hasFile('img1')) {
            $file=Input::file('img1');
            $control=0;
            $nombre = $file->getClientOriginalName();
            while ($control == 0) {
                if (is_file( public_path().'/imagenes/perfil/'.$nombre )) {
                    $nombre = rand() . $nombre;
                }else{
                    $file->move(public_path().'/imagenes/perfil/',$nombre);
                    $empresa->img1=$nombre;
                    $control=1;
                }
            }
        }
}

Любая помощь, как я могу это сделать?В виде картинок я их подбираю вот так

<ion-item *ngIf="plan > 2">
      <ion-label position="floating">Imagen Ciete</ion-label>
      <ion-input type="file"
                name="foto7"
                [(ngModel)]="adicional.foto7"
                >
      </ion-input>
  </ion-item>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...