skip to Main Content

For an order system I made using Laravel users are able to upload files related to an order. These are send via an ajax call and processed by a Laravel controller, after which the page is reloaded.

Now the problem which I’m having is that at a certain point, after an user has uploaded some files in a short period of time and they want to upload another file, they are greeded with an ERR_CONNECTION_TIME_OUT. Note that the file is uploaded succesfully each time.

I checked the Apache and Laravel error logs and there is nothing relevant there. I also checked the php.ini upload limits and execution times and I don’t think they cause any issue as they are set to 128Mb (they only upload PDFs), and 30sec respectively.

Am I missing something here?

Ajax:

$("#upload").on( "click", function() {
        var files = $('input[name=global_file]')[0].files;
        var fd = new FormData();
        fd.append('global_file',files[0]);
        fd.append('tm_num', {{ $order['tm_num'] }});

        $.ajax({
            type: "POST",
            url: '/admin/order/upload',
            data: fd,
            processData: false,
            contentType: false,
            success: function () {
                window.location.reload();
            }
        });
    });

Controller.php:

public function UploadOrderFile(Request $request)
    {
        $data = $request->all();
        $file = $request->file('global_file');

        $basepath = base_path();
        $destinationPath = $basepath.'/orders/'.$data['tm_num'];
        $fileName = $file->getClientOriginalName(); 

        $file->move($destinationPath,$fileName);
        
        return;
    }

2

Answers


  1. $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    });
    
    public function UploadOrderFile(Request $request)
        {
            $data = $request->all();
      if(!$request->hasFile('global_file')) return false;
    
            $file = $request->file('global_file');
    
            $basepath = base_path();
            $destinationPath = $basepath.'/orders/'.$data['tm_num'];
            $fileName = $file->getClientOriginalName(); 
    
            $request->store($destinationPath."/".$fileName);
            
            return true;
        }
    
    Login or Signup to reply.
  2. On ajax headers, need to set the csrf token to access the request.

    $("#upload").on( "click", function() {
            var files = $('input[name=global_file]')[0].files;
            var fd = new FormData();
            fd.append('global_file',files[0]);
            fd.append('tm_num', {{ $order['tm_num'] }});
    
            $.ajax({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                },
                type: "POST",
                url: '/admin/order/upload',
                data: fd,
                processData: false,
                contentType: false,
                success: function () {
                    window.location.reload();
                }
            });
        });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search