如何将裁剪图像从Ajax发送到Symfony控制器?

时间:2018-06-07 15:20:24

标签: php ajax symfony symfony-3.3 symfony-3.4

我一直坚持使用Symfony中的图像裁剪。我正在使用Croppie js进行裁剪,但我无法将裁剪后的图像保存到数据库中..

<script>  

  $(document).ready(function(){
   $image_crop = $('#image_demo').croppie({
enableExif: true,
viewport: {
  width:200,
  height:200,
  type:'square' 
},
boundary:{
  width:300,
  height:300
}
  });
      $('#upload_image').on('change', function(){
var reader = new FileReader();
reader.onload = function (event) {
  $image_crop.croppie('bind', {
    url: event.target.result
  }).then(function(){
    console.log('jQuery bind complete');
  });
}
reader.readAsDataURL(this.files[0]);
$('#uploadimageModal').modal('show');
  });
    $('.crop_image').click(function(event){
$image_crop.croppie('result', {
  type: 'canvas',
  size: 'viewport'

}).then(function(response){

   var file = response.replace(/^data:image\/(png|jpg);base64,/, "");
var data = new FormData();
data.append('img', file);
console.log(file);


  $.ajax({

    url: "{{ path('app_bundle_route') }}",
    type: "POST",
    data: data,
    processData: false,
    contentType: false,
    cache: false,
    success:  function(data){
           $('#uploadimageModal').modal('hide');
           $("#previewImg").attr('src', response);

    }
  });
});
 });

});  

如何在控制器中获取裁剪后的图像? 代码有什么问题吗?

console.log(file);
iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAYAAACtWK6eAAAgAElEQVR4nGS8x5Ik15auB3b3aQBVmaGVh8zQWuvw0FqlrKrUOiIzI3VpCRRKATgHp9FH9m3e5pBjCiPNrvG+AQecccwH4IDP8HHgUQHA7mDbDveMNMuBf/mv/19r+xeOgAVPxI4nYsMXs+MMmvGGbdJ11Ikv6mAhaELn06D0y1EHlRhTehZKVsyiCX1Sh9anRmGToXQpERImdDEtlqyAIaVFyBgwpPXYylYMBT3GihFnz4F9aMW5tIBzdQHnih3LwIK1ayX4IIwyqEJllKPQy5kzzCET7qLSzyHXz6EQ5pHr55Br5lHo5pjXfiXdN8yjFGSzpTDMIxPm0VhUCAtqlMa7KG3zKJxyNB4V+qAWU1jAGNGiCasx+rUEIgtkSmEypSAp0U9a9JIvB0gXPKSKXtIlH5GMnUzZT1KU7iVFD7lqkHwtQL7sI13wEIpbyYhe8mUfGdFNMGYkmXOQLrjIiG7SBRdiNUCh4qdQ8SNWpd/Nl30Ua8HZ/WItiFgNINZ8FOs+xJqXQtVDsS5dl+v+Xy0v1aafatNPvR2k1gpQa/qp1L1U6l5qTT+1pp9qw0erG6beCkzv+6g1vbTaXpotD9Wqm0rFTbPpo9cL0+kEabX8NFsehosR+oMQK6sJVlYTLK/EWFmNs76R5f6DNItLEYaLEVbuJVjfyLJ2L879+wkerKfY3MqysZVkayfF9m6ara0k+/s5traSbG+n2N3NsLufZWcvx/Zulv1DkfX1OPv7OTY3E+zsZNncTHB4lOfouMDoqMDhfpbRUW66FxgdiRwdFdjdSzMaFzgdi2xtRDg6yHB2UmQ8SnN1KXJzXePirMjFWYHJWZaz0xxHB1Euz/NcX4rc3BS5eVjm4eMaXziDVnwxJ97oAp6IDVfIgidkxROx4Y048YbtmH0GbCkrCp8SU0JAGZChj2vRRbUonQqUVjky4zzzpnlU…

1 个答案:

答案 0 :(得分:0)

我不知道裁剪了什么,但我可以看到你通过ajax发送文件。

您可以使用Request类在symfony中获取文件,请求属性等。

$request->files->get('name_of_your_parameter_sent')

如果您不确定发送的参数的名称是什么

$request->files->all()

此处有更多信息Http foundation