亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Laravel 文件上傳時無法識別文件錯誤“Call to a member function ”

Laravel 文件上傳時無法識別文件錯誤“Call to a member function ”

PHP
夢里花落0921 2023-10-21 15:47:20
我正在使用 Laravel、Ajax 和 Datatables 來管理我的信息,一切正常,但我需要將文件上傳到其中。但我收到 Call to a member function store() on null。這是控制器功能的代碼(post 和 fetch):    function postdata(Request $request)    {        $validation = Validator::make($request->all(), [            'referencia'  => 'required',            'tipo_equipo'  => 'required',            'tipo_servicio'  => 'required',            'id_reporte'  => 'required',        ]);        $error_array = array();        $success_output = '';        if ($validation->fails())        {            foreach($validation->messages()->getMessages() as $field_name => $messages)            {                $error_array[] = $messages;            }        }        else        {            if($request->get('button_action') == 'update')            {                $servicio = Servicio::find($request->get('servicio_id'));                $servicio->referencia = $request->get('referencia');                $servicio->tipo_equipo = $request->get('tipo_equipo');                $servicio->tipo_servicio = $request->get('tipo_servicio');                $servicio->id_reporte = $request->get('id_reporte');                $servicio->imagen_inicio = $request->file('imagen_inicio')->store('public/img/servicio');                $servicio->imagen_fin = $request->get('imagen_fin');                $servicio->pdf_reporte = $request->get('pdf_reporte');                $servicio->save();                $success_output = '<div class="alertaTables alert alert-success">Servicio Actualizado</div>';            }        }        $output = array(            'error'     =>  $error_array,            'success'   =>  $success_output        );        echo json_encode($output);    }
查看完整描述

1 回答

?
阿波羅的戰車

TA貢獻1862條經驗 獲得超6個贊

它不允許您存儲文件,因為文件無法發送到服務器,因為您使用 AJAX 發出請求,要通過 AJAX 發送文件,請嘗試以下操作:


$('#servicio_form').on('submit', function(event){

   event.preventDefault();


   // init formData and get files

   let formData = new FormData($('#servicio_form')[0]);

   let imagenInicio = $('#imagen_inicio')[0].files[0];

   let imagenFin= $('#imagen_fin')[0].files[0];

   let pdfReporte= $('#pdf_reporte')[0].files[0];


   // append files to formData

   formData.append('imagen_inicio', imagenInicio);

   formData.append('imagen_fin', imagenFin);

   formData.append('pdf_reporte', pdfReporte);


   // in AJAX disable contentType, processData and cache

   $.ajax({

      url:"{{ route('mis-servicios.postdata') }}",

      method:"POST",

      dataType:"json",

      data: formData,       // change this and the following options

      contentType: false, 

      processData: false,

      cache: false,

      success: function(data) {

         // your code...

      }

   });

});


查看完整回答
反對 回復 2023-10-21
  • 1 回答
  • 0 關注
  • 265 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號