vendredi 28 décembre 2018

Chunked file uploads with Laravel & AJAX

I try to create a script to upload large files, I send data through Ajax (It's work), but this did not solve the problem of large files, in the best circumstances, I can upload only 100 MB, and this is not good for the user, can you help me to add Chunked file uploads, Thank you

This is my HTML:

<form id="upload_form" method="POST" enctype="multipart/form-data">
 @csrf
 <input type="file" name="file" id="file-7" class="inputfile inputfile-6" />
<center>
   <a href="#" id="star_up" value="hide/show">
      <input class="btn btn-info btn-lg wow tada" id="sell_house_submit" type="submit" name="upload_file" value="@lang('lang.UPLOAD')">
   </a>
</center>
<div id="prog" style="display:none;">
    <progress id="progressBar" class="wow tada" style="@if(isMobile())width:200px;@else width:750px;@endif height: 10px;" value="0" max="100">
    </progress>
    <h3 class="av" id="status" ></h3>
    <p id="loaded_n_total" ></p>

My javascript:

$(document).ready(function(){

 $('#upload_form').on('submit', function(event){
   $.ajax({
   url:"",
   method:"POST",
   data: new FormData(this),
   xhr: function() {
        var myXhr = $.ajaxSettings.xhr();
        if(myXhr.upload){
            myXhr.upload.addEventListener('progress',progressHandler, false);
            myXhr.addEventListener("load", completeHandler, false);
            myXhr.addEventListener("error", errorHandler, false);
            myXhr.addEventListener("abort", abortHandler, false);
        }
        return myXhr;
    },
   dataType:'JSON',
   contentType: false,
   cache: false,
   processData: false,
   // IF SUCCESS
   success:function(data)
   {
      if (data.type_is == 'swal') {
        swal({
            title: data.title,
            html: data.html,
            showCloseButton: true,
            focusConfirm: false,
            type: data.status
        });
        jQuery('#progress_up').toggle('hide');
        document.getElementById('file-7').value = '';
        document.getElementById('name_file').innerHTML = '';
      } 
   }
  })
    event.preventDefault();
 });

});

Controller:

protected function upload(Request $request) {
  ini_set('max_execution_time', 3000);
  ini_set('memory_limit','256M');

  // UPLOAD FILE
  if (getValue('storage_type', 'disk') == 's3') {
     $path = Storage::disk('s3')->putFile('files', $request->file('file'));
     $storage = 's3';
  } else {
  $path = Storage::putFile('files', $request->file('file'));
  $storage = 'disk';
  }
  // RANDOM
  $link_id = Random();
  $delete_token = Random(50);
  $preview = Random(10);
  $ip = getip();

  $this->createFile($name, $path, $link_id, $delete_token, $ip, $preview, $size, $mime, $type, $storage);

  // FILE UPLOADED

  return response()->json([
     'title' => trans('lang.GREAT'),
     'html' => getFileInfoHTML($link_id, $delete_token, $name, $type, $preview),
     'type_is' => 'swal',
     'status' => 'success'
  ]);
}

I put the most important things only, I did not put the details of the file review and size ...., I just want to know how I can add the property and upload files without a large RAM :)



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire