2

I'm trying to implement the blueimp jquery file upload with a laravel app. https://github.com/blueimp/jQuery-File-Upload

I've set up the form which is working and behaving as expected using the plugin but I'm having problems creating the server side scripts in laravel to handle the upload.

I've changed the form action as follows:

<form id="fileupload" action="{{ route('photos.post.upload') }}" method="POST"
                              enctype="multipart/form-data">

in main.js (part of the plugin) I've set the url to:

$('#fileupload').fileupload({
    // Uncomment the following to send cross-domain cookies:
    //xhrFields: {withCredentials: true},
    url: '/photos/upload/'
});

In my routes file I have created a route as follows:

Route::any('photos/upload', [
    'as'=>'photos.post.upload',
    'uses' => 'PhotosController@uploadImage'
]);

My function in the controller:

public function uploadImage()
{
    dd(Input::file());

    return Response::json(
        array(
            "files" => array(
                "name" => "post"
            ))
    );
}

At this point I simply want to test the form data is received. However on upload the data is empty.

Using either Route::any() or Route::post() but get a 301 error (permanently moved)

Checking in google chrome it appears POST is being used for the upload as expected and looks like the file is included.

So.

  1. Why am I getting the 301 error - what am I missing from either the javascript side
  2. Why does Input::file() return empty

Any help appreciated

3
  • Laravel redirects if a trailing slash is present in the URL, like you have at url: '/photos/upload/', so it’ll be redirected with a 301 status to the URL without the trailing slash. Commented Mar 1, 2015 at 22:01
  • Aghhhhhh! THANK YOU - spent so much time on this and didn't spot that. Updated the main.js and all working. . Commented Mar 1, 2015 at 22:05
  • Great! :) Added it as an answer too. Commented Mar 1, 2015 at 22:06

1 Answer 1

5

Laravel redirects if a trailing slash is present in the URL, like you have at url: '/photos/upload/', so it’ll be redirected with a 301 status to the URL without the trailing slash.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.