首页 > 解决方案 > 在 Symfony 4 中使用 Ajax 上传裁剪图像时,在 null 上调用成员函数 getData()

问题描述

所以在这里我裁剪图像并发送到 Symfony 控制器......

 $(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){

  $.ajax({

    url: "{{ path('app_bundle_route') }}",
    type: "POST",
    data: {'img': response},

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

    }
  });
 });
});

 });  

控制器

        $file = $request->files->get('img');   

        $fileName = md5(uniqid()).'.'.$file->guessExtension();

        $file->move($this->getParameter('image_directory'), $fileName);

        $imageEn->setImage($file);
        $em->persist($imageEn);
        $em->flush();

我犯了什么错误吗?我正在尝试将图像从 Ajax 发布到 Symfony 4,但控制器似乎无法获取从 Ajax 发送的图像文件。谁能帮助我如何将图像从 Ajax 发送到控制器?

标签: phpajaxsymfonysymfony-3.3croppie

解决方案


推荐阅读