1

I am trying to use FileDrop.js (latest version) along with Laravel 5.1 in order to upload files.

I have set a route for the ajax request inside routes.php:

Route::any('/fileupload/ajax', ['as' => 'fileupload.ajax', 'uses' => 'FileUpload@ajaxRequest']);

and that jquery code to catch file drop in my respective blade:

$('.filedrop')
     .filedrop()
     .on('fdsend', function (e, files) {
         var ajaxUrl = '{{ route('fileupload.ajax') }}';
         //code here
         files.invoke('sendTo', ajaxUrl);
     })
     .on('filedone', function (e, file) {
         //code here
     });

The problem is that ajax call returns Laravel's "Page not found!".

It seems like FileDrop.js bypass laravel routing.

Any ideas.

giorgos
  • 1,771
  • 4
  • 15
  • 14
  • Try changing `/fileupload/ajax` to `fileupload/ajax`, I don't think the first slash should be there – Fester Jan 26 '16 at 15:05
  • @Fester It is absolutely the same. – giorgos Jan 26 '16 at 15:12
  • What happens if you navigate to the route with your browser? – Fester Jan 26 '16 at 15:15
  • @Fester It works, returns FileUpload@ajaxRequest as it should be. The problem is that seems like FileDrop bypasses laravel routing. – giorgos Jan 26 '16 at 15:18
  • There's no way to 'bypass' laravel routing, all laravel routing does is provide endpoints in the form of URL's which are connected to controller functions – Fester Jan 26 '16 at 15:25
  • 1
    Try using `url('fileupload/ajax')` instead of `route('fileupload.ajax')`. Also, is your jquery sending a post request? In that case you need to include a csrf token – Fester Jan 26 '16 at 15:29
  • @Fester You are right, thank you. It was lack of csrf token that caused that problem. – giorgos Jan 26 '16 at 15:51

1 Answers1

1

This question was solved in the comments but Im reposting the answer here for completeness sake.

Giorgos forgot to send a CSRF token with the POST request, Laravel requires this by default on all POST request as is explained in the Laravel documentation.

Fester
  • 863
  • 1
  • 12
  • 33