Trouble implementing Uploadify with Laravel 4

612 views Asked by At

EDITED:

I want to use uploadify in laravel 4. I am stuck since i only get the 500 Http error and i haven't found out why.

So here is my code so far:

The form:

{{Form::open(array('action' => 'ProfileController@postUploadPicture2','files'=> true,'id'=>'uploadform2'))}}

            {{ Form::file('file_upload',$attributes = array('id'=>'file_upload')) }}

{{ Form::token() }}

{{ Form::close() }}

The uploadify script:

<script>
$(document).ready(function() {
    var url = $("#uploadform2").attr('action');
    setTimeout(function() {//referencias: http://stackoverflow.com/a/25135325/1883256
        $('#file_upload').uploadify({
            'fileTypeDesc' : 'Image Files',
            'fileTypeExts' : '*.gif; *.jpg; *.png',
            'debug'        :'true',
            'swf'      : '../swf/uploadify.swf',
            'uploader' : url, //debe redireccionar al controlador
            'onFallback' : function() {
                                    alert('Flash player no detected.');
                                    },
            'method'   : 'post',
            'width'    : 150,//ancho del botón de browser
            'buttonText' : 'Choose your file...',
            'onUploadError' : function(file, errorCode, errorMsg, errorString) {
                                            alert(':-( The file ' + file.name + ' could not be sent. There was the following error: ' + errorString + ' Mensaje: '+ errorMsg + ' código: '+errorCode + ' File: '+ file);
                                            }
            // Put your options here
        });
    },0);
});
</script>

The routes:

/*
*Upload user's picture AJAX (GET) 
*/
Route::get('/edit/upload_picture2',array(
        'as'    => 'user-upload-photo2',
        'uses'  => 'ProfileController@getUploadPicture2'
));

        /*
    /Upload user's picture AJAX Uploadify (POST)
    */
    Route::post('/edit/upload_picture2',array(
        'as'    => 'user-upload-photo-post2',
        'uses'  => 'ProfileController@postUploadPicture2'
    ));

And the controller (in testing mode) where i expect to show me a message:

public function postUploadPicture2(){
return 'posted and reached!';     
}

I get the HTTP Error 500 and code -200.

The debug info shows the following: Opening uploadify:

---SWFUpload Instance Info---
Version: 2.2.0 2009-03-25
Movie Name: SWFUpload_0
Settings:
    upload_url:               http://localhost/cimmgm/public/edit/upload_picture2
    flash_url:                ../swf/uploadify.swf?preventswfcaching=1420936888141
    use_query_string:         false
    requeue_on_error:         false
    http_success:             
    assume_success_timeout:   30
    file_post_name:           Filedata
    post_params:              [object Object]
    file_types:               *.gif; *.jpg; *.png
    file_types_description:   Image Files
    file_size_limit:          0
    file_upload_limit:        0
    file_queue_limit:         999
    debug:                    true
    prevent_swf_caching:      true
    button_placeholder_id:    file_upload
    button_placeholder:       Not Set
    button_image_url:         /cimmgm/public/edit/
    button_width:             150
    button_height:            30
    button_text:              
    button_text_style:        color: #000000; font-size: 16pt;
    button_text_top_padding:  0
    button_text_left_padding: 0
    button_action:            -110
    button_disabled:          false
    custom_settings:          [object Object]
Event Handlers:
    swfupload_loaded_handler assigned:  false
    file_dialog_start_handler assigned: true
    file_queued_handler assigned:       true
    file_queue_error_handler assigned:  true
    upload_start_handler assigned:      true
    upload_progress_handler assigned:   true
    upload_error_handler assigned:      true
    upload_success_handler assigned:    true
    upload_complete_handler assigned:   true
    debug_handler assigned:             true

SWF DEBUG: SWFUpload Init Complete
SWF DEBUG: 
SWF DEBUG: ----- SWF DEBUG OUTPUT ----
SWF DEBUG: Build Number:           SWFUPLOAD 2.2.0
SWF DEBUG: movieName:              SWFUpload_0
SWF DEBUG: Upload URL:             http://localhost/cimmgm/public/edit/upload_picture2
SWF DEBUG: File Types String:      *.gif; *.jpg; *.png
SWF DEBUG: Parsed File Types:      gif,jpg,png
SWF DEBUG: HTTP Success:           0
SWF DEBUG: File Types Description: Image Files (*.gif; *.jpg; *.png)
SWF DEBUG: File Size Limit:        0 bytes
SWF DEBUG: File Upload Limit:      0
SWF DEBUG: File Queue Limit:       999
SWF DEBUG: Post Params:
SWF DEBUG: ----- END SWF DEBUG OUTPUT ----
SWF DEBUG: 

Attempting to upload the file:

SWF DEBUG: Event: fileDialogStart : Browsing files. Multi Select. Allowed file types: *.gif; *.jpg; *.png
SWF DEBUG: Select Handler: Received the files selected from the dialog. Processing the file list...
SWF DEBUG: Event: fileQueued : File ID: SWFUpload_0_0
SWF DEBUG: Event: fileDialogComplete : Finished processing selected files. Files selected: 1. Files Queued: 1
SWF DEBUG: StartUpload: First file in queue
SWF DEBUG: Event: uploadStart : File ID: SWFUpload_0_0
SWF DEBUG: ReturnUploadStart(): File accepted by startUpload event and readied for upload.  Starting upload to http://localhost/cimmgm/public/edit/upload_picture2 for File ID: SWFUpload_0_0
SWF DEBUG: Event: uploadProgress (OPEN): File ID: SWFUpload_0_0
SWF DEBUG: Event: uploadProgress: File ID: SWFUpload_0_0. Bytes: 39446. Total: 39446
SWF DEBUG: Event: uploadError: HTTP ERROR : File ID: SWFUpload_0_0. HTTP Status: 500.
SWF DEBUG: Event: uploadComplete : Upload cycle complete.

How can i fixe it? What am i missing?

2

There are 2 answers

1
Elijan On

Your route name here is "user-upload-photo-post'

   Route::post('/edit/upload_picture',array(
                'as'    => 'user-upload-photo-post',
                'uses'  => 'ProfileController@postUploadPicture'
            ));

while I don;t see the form tag in the veiw that posts to this route. E.g.

Form::open(array('action'=>route('user-upload-photo-post'), 'method'='post'))

edit: ah got it, you post to same page. is a crf token posted as well??

0
BaoAnhNg On

A bit late but if someone get stuck with the same problem then it might help.

Uploadify use flash object which has different session so csrf token doesn't work. This is the reason Laravel returns 500 Http error. There are two ways to overcome I've tried: