dimanche 21 juin 2020

Problem in uploading file form with ajax and laravel?

i have a simple form to upload file and it use ajax to send the request to the laravel function

The problem with the form its not passing the file it pass only the CSRF token

it tried to change the ajax formData and pass the form ID instead of calling the form array but have no luck at it all

blade file

<div class="slim-mainpanel">
    <div class="container">
      <div class="slim-pageheader">
        <ol class="breadcrumb slim-breadcrumb">
          <li class="breadcrumb-item"><a href="/">الصفحة الرئيسية</a></li>
            <li class="breadcrumb-item"><a href="/products">إدارة المنتجات</a></li>
          <li class="breadcrumb-item active" aria-current="page">إضافة منتج</li>
        </ol>
        <h6 class="slim-pagetitle">إضافة منتج</h6>
      </div><!-- slim-pageheader -->

      <div class="section-wrapper">
        <label class="section-title">إضافة منتج</label>
        <p class="mg-b-20 mg-sm-b-40">استخدم النموذج التالى لاضافة منتجات جديدة </p>
        <div role="alert" id="alert" style="display: none;">
          <button type="button" class="close" data-dismiss="alert" aria-label="Close">
            <span aria-hidden="true">×</span>
          </button>
          <div id="message"></div>
        </div>
        <form name='productsAdd' id='productsAdd' enctype="multipart/form-data">
          <div class="tx-right">
            <div class="d-md-flex mg-b-30">
              <div class="form-group mg-b-0">
               <div class="custom-file">
                  <label class="custom-file-label custom-file-label-primary" for="customFile">اختر ملف الاكسل</label>
                  <input type="file" class="custom-file-input" id="customFile2">
                </div>
              </div><!-- form-group -->
            </div><!-- d-flex -->
          </div>
          <div class="tx-right">
            <button class="btn btn-primary pd-x-20" id="but_upload" type="submit">إضافة منتج</button>
          </div>
        </form>
      </div><!-- section-wrapper -->

ajax call

<script>
  $('form').submit(function(event) {
          event.preventDefault();
          var formData = new FormData($(this)[0]);
          formData.append('_token', '');
          $.ajax({
              url: '',
              type: 'POST',              
              data: formData,
              cache: false,
              contentType: false,
              processData: false,
              success: function(result)
              {
                console.log(result);
              },
              error: function(data)
              {
                  console.log(data);
              }
          });  
      }); 
</script>

laravel function

public function import(Request $request)
    {
        dd($request->all());
    }

output

array:1 [
  "_token" => "1WQZ0755DuZYpvwdP0rYTvRoVF6Nnc1bw2DH0GJF"
]


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire