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>