2

i would like to know if it is possible to make a file upload form and send the data via ajax with symfony2 ?

here is my twig view:

<form class="upavatar" method="post" action="{{ path('sbcUserBundle_avatar', {'id': app.security.getToken().getUser().getId()}) }}" {{ form_enctype(form) }}>
  {{ form_end(form) }}
  {{ form_errors(form) }}
</form>
</div>
<script>          
 $('.upavatar').submit(function() 
 {
  $.post($(this).attr('action'), $(this).serialize(), function(data) {
        $("#upmyavatar").empty();
        $("#upmyavatar").html(data);
  });
  return false;
}); 
</script>

Because i did a form upload file within symfony2 and without ajax (jquery actually) it works like a charm, but when i try to send it trough ajax the form handler can't find the file variable (i checked with xdebug).So i'am starting to wonder if it's possible to send a file via ajax within symfony2, in the past i successfully used ajax for "normal" form.

Thanks.

ok i got it working by doing that:

<script>
$('#myForm').live('change', function()
{
    var options = {
    beforeSend: function()
    {
        $("#progress").show();
        //clear everything
        $("#bar").width('0%');
        $("#message").html("");
        $("#percent").html("0%");
    },
    uploadProgress: function(event, position, total, percentComplete)
    {
        $("#bar").width(percentComplete+'%');
        $("#percent").html(percentComplete+'%');
    },
    success: function()
    {
        $("#bar").width('100%');
        $("#percent").html('100%');
    },
    complete: function(response)
    {
        $(".myavatarupload").empty();
        $(".myavatarupload").html("<font color='green'>"+response.responseText+"</font>");
    },
    error: function()
    {
        $("#message").html("<font color='red'> ERROR</font>");
    }
};
     $("#myForm").ajaxForm(options);
});
</script>
Seriescw Esxille
  • 301
  • 2
  • 5
  • 12
  • It is possible, I have already made it (I can't find the code quickly), but take a look at this question, it may help you : http://stackoverflow.com/questions/166221/how-can-i-upload-files-asynchronously-with-jquery – Pierrickouw Aug 03 '13 at 14:10

0 Answers0