Javascript 为什么在php中使用cropper js上传图像时不需要工作

Javascript 为什么在php中使用cropper js上传图像时不需要工作,javascript,php,jquery,file-upload,cropperjs,Javascript,Php,Jquery,File Upload,Cropperjs,大家好,我正在建设一个项目,我已经使用cropper js裁剪上传的图像一切都很好,我的代码,如文件被裁剪上传到数据库,但问题是,当我想检查文件字段不应该是空的,我使用的是必需的,但它不工作,我粘贴的代码我一直用到现在你们能看看我需要做什么来纠正错误吗 HTML PHP 你可以这样做,我希望它能起作用 var image = $('#image').val(); if(image == ''){ toastr.error("Please upload a file&q

大家好,我正在建设一个项目,我已经使用cropper js裁剪上传的图像一切都很好,我的代码,如文件被裁剪上传到数据库,但问题是,当我想检查文件字段不应该是空的,我使用的是必需的,但它不工作,我粘贴的代码我一直用到现在你们能看看我需要做什么来纠正错误吗

HTML

PHP


你可以这样做,我希望它能起作用

var image = $('#image').val();

  if(image == ''){
      toastr.error("Please upload a file",'Error');
    }else{

      resize.croppie('result', {
      type: 'canvas',
      size: 'viewport'
    }).then(function (img) {


      $.ajax({
        url: "model/product_image/product_image.php?insertimg&imgid="+prod_id,
        type: "POST",
        data: {"image":img},
        success: function (data) {
         var res = data;
           if(res==8){
              toastr.error("Sorry! There is an error",'Error');
            }else if(res==9){
              toastr.error("Please upload a file",'Error');
            }else{
             toastr.success(data,'Update');
             $('#partyins')[0].reset();

           }
        }
      });
    });

    }


“它不工作”是什么意思?到底发生了什么?@Wesley Smith实际上该文件正在被上传空白。我想要的是,如果没有提供该文件,它就不应该被上传uploaded@Dharman是的,我知道在调用
resize.cropie之前检查输入(“结果”…
并作出反应accordingly@charlietfl据你说,它显示toastr错误“请上传一个文件”,但随后alos被上传。
var resize = $('#upload-demo').croppie({
    enableExif: true,
    enableOrientation: true,    
    viewport: { // Default { width: 100, height: 100, type: 'square' } 
        width: 64,
        height: 64,
        type: 'circle' //square
    },
    boundary: {
        width: 300,
        height: 300
    }
});


$('#image').on('change', function () { 
  var reader = new FileReader();
    reader.onload = function (e) {
      resize.croppie('bind',{
        url: e.target.result
      }).then(function(){
        console.log('jQuery bind complete');
      });
    }
    reader.readAsDataURL(this.files[0]);
});


$("#partyins").submit(function(e){
  e.preventDefault();

  var prod_id = $('#product_id').val();

  resize.croppie('result', {
    type: 'canvas',
    size: 'viewport'
  }).then(function (img) {

    if(img == ''){
      toastr.error("Please upload a file",'Error');
    }

    $.ajax({
      url: "model/product_image/product_image.php?insertimg&imgid="+prod_id,
      type: "POST",
      data: {"image":img},
      success: function (data) {
       var res = data;
         if(res==8){
            toastr.error("Sorry! There is an error",'Error');
          }else if(res==9){
            toastr.error("Please upload a file",'Error');
          }else{
           toastr.success(data,'Update');
           $('#partyins')[0].reset();

         }
      }
    });
  });
});


if(isset($_GET['insertimg']) && isset($_GET['imgid'])){

  $id = $_GET['imgid'];

  $image = $_POST['image'];

  list($type, $image) = explode(';',$image);
  list(, $image) = explode(',',$image);

  $image = base64_decode($image);
  $image_name = 'product_id'.$id.'_'.time().'.png';
  file_put_contents("../../../upload_products/".$image_name, $image);

  if(!$image == ''){

    $sql_inst = "UPDATE $tb_products SET `p_upload`='$image_name' WHERE id='$id'";
    $res_inst = mysqli_query($conn,$sql_inst);   
    if($res_inst){
      echo "Updated Successfully";

    }else{
      echo 8;
    } 

  }else{

    echo 9;

  }

  


}
var image = $('#image').val();

  if(image == ''){
      toastr.error("Please upload a file",'Error');
    }else{

      resize.croppie('result', {
      type: 'canvas',
      size: 'viewport'
    }).then(function (img) {


      $.ajax({
        url: "model/product_image/product_image.php?insertimg&imgid="+prod_id,
        type: "POST",
        data: {"image":img},
        success: function (data) {
         var res = data;
           if(res==8){
              toastr.error("Sorry! There is an error",'Error');
            }else if(res==9){
              toastr.error("Please upload a file",'Error');
            }else{
             toastr.success(data,'Update');
             $('#partyins')[0].reset();

           }
        }
      });
    });

    }