Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

blueimp/jQuery-File-Upload with Laravel How to integrate?

Trying to build my upload images part of my site and wanted to use blueimp/jQuery-File-Upload instead of hardcoding everything from scratch. However I am new too all that, could you tell me HOW to integrate that plugin with my Laravel structure ?

Where do I put all the files ? In vendors folder ? Or should I split all the folders and put their js folder in mine etc???

If you know a tutorial it is even better... Couldn't find anything good with google.

Thanks

like image 415
commandantp Avatar asked Jul 20 '14 11:07

commandantp


1 Answers

You can try this code I'm posting to help others.

The first step is to define the upload page and upload handling Routes, like this:

Route::get('image_', function() {
    return View::make('image.upload-form');
});

Route::post('image_updade', 'ImageController@postUpload');

Make your image.upload-form view something like this (I'm using simple HTML, not a Blade template):

<?php echo Form::open(array('url' => 'image_updade', 'files' => true, 'id' => 'myForm')) ?> 
    Name: <input type='file' name='image' id='myFile'/>
    <br/>
    Comment: <textarea name='comment'></textarea>
    <br/>
    <input type='submit' value='Submit Comment' /> 
<?php echo Form::close() ?>

Now you need to add the JavaScript files in that view page's <HEAD> tag:

<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js'></script> 
<script src='http://malsup.github.com/jquery.form.js'></script> 

<script> 
    // Wait for the DOM to be loaded 
    $(document).ready(function() {

        // Bind 'myForm' and provide a simple callback function 
        $('#myForm').ajaxForm(function() {
            alert('Thank you for your comment!');
        });

        $('#myFile').change(function() {
            $('#myForm').submit();
        });
    });
</script> 

Finally, here's a simple example of code for the ImageController@postUpload controller to get the uploaded file, and move it to a destination folder:

<?php

    class ImageController extends BaseController {

        public function getUploadForm() {
            return View::make('image/upload-form');
        }

        public function postUpload() {

            $file = Input::file('image');
            $input = array('image' => $file);
            $rules = array( 'image' => 'image');
            $validator = Validator::make($input, $rules);

            if ( $validator->fails() ){
                return Response::json(['success' => false, 'errors' => $validator->getMessageBag()->toArray()]);
            }
            else {
                $destinationPath = 'files/';
                $filename = $file->getClientOriginalName();
                Input::file('image')->move($destinationPath, $filename);
                return Response::json(['success' => true, 'file' => asset($destinationPath.$filename)]);
            }
        }
    }
like image 61
HaRsH Avatar answered Oct 18 '22 21:10

HaRsH