سؤال

i m trying to use Uploadify to handle my file uploading in Symfony2. The image uploading should call a controller, inside the controller it will get the current login user, create subdirectories based on the current username, and then move the file to that folder. The problem is when i run a $user->getUsername(), or any other functions related to the $user object, the upload fails with a HTTP 500 error, however everything works ok when i run this controller from the URL.

Here's some code:

Frontend configuration for uploadify:

<script type="text/javascript">
    $(document).ready(function() {
        $('#fileupload').uploadify(
            {
                'swf': "{{ asset('javascript/uploadify.swf') }}",
                'uploader': "{{ path('EasyLife_upload_avatar')}}",
                'auto': true,
                'buttonText': "Upload Avatar",
                "buttonClass": "label",
                "height": 30,
                "width": 80,
                "onUploadSuccess": function(file, data, response) {
                    alert("Successful " + file.name + " to :" + data + ":" + response);
                },
                'onUploadError' : function(file, errorCode, errorMsg, errorString) {
                    alert('The file ' + file.name + ' could not be uploaded: ' + errorString);
                }
            });
    });
</script>

Here's the controller that handles the upload:

public function uploadAvatarAction() {
    $user = $this->getUser();
    $user->getUsername(); // problem appears when this line is added on an Ajax call
    $targetFolder = "upload/avatar";

    return new Response($targetFolder);
}

I actually have $user->getUsername() all over the project in other controllers, they are all good, even this one works on direct URL call, but problem occurs when called by Ajax, how strange?

Can anyone help? Thanks a lot!

هل كانت مفيدة؟

المحلول

This is because uploadify does not send the session cookie with the request, so in your controller $user = $this->getUser(); is null.

You can see a similar problem I had here...

You need to create a custom session storage...

<?php

namespace Acme\AcmeBundle\Session;

use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\SessionStorage\NativeSessionStorage;

class Storage extends NativeSessionStorage
{
    public function __construct(array $options = array(), ContainerInterface $container)
    {
        $request = $container->get('request');
        if ($request->query->has('sessionId')) {
            $request->cookies->set(session_name(), 1);
            session_id($request->query->get('sessionId'));
        }

        return parent::__construct($options);
    }
}

And configure your service container....

parameters:
    session.storage.native.class: Acme\AcmeBundle\Session\Storage
services:
    session.storage.native:
        class:     %session.storage.native.class%
        arguments: [%session.storage.options%, @service_container]
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top