繁体   English   中英

在ajax中调用上传功能后,图片未上传

[英]image not uploaded after calling upload function in ajax

我正在处理一张图片上传表单,该表单将通过nudejs脚本验证上传的图片是否包含裸露。
如果图片不包含裸露,则会上传图片。
上传和检查裸露功能运行良好,但是问题是我没有找到链接这两种方法的方法,因为检查裸露功能“ onImageClick()”的返回为'undefined'所以我无法检查如果为true或false,则通过$.ajax调用上载函数。

这是代码:

$(document).ready(function (e) {
    $("#uploadimage").on('submit',(function(e) {
        e.preventDefault();
        //return onImageClick('previewing'); //previewing is the id of the image in my html file
        //if(onImageClick('previewing')) return;
        var rn = onImageClick('previewing');
        console.log("rn: "+rn); //always get undefined 
        $("#message").empty();
        $('#loading').show();

        $.ajax({
          url: "ajax_php_file.php",
          type: "POST",            
          data: new FormData(this),
          contentType: false,      
          cache: false,            
          processData:false,       
          success: function(data) {
             $('#loading').hide();
             $("#message").html(data);
          }
      });
}));

// Function to preview image after validation
$(function() {
    $("#file").change(function() {
        $("#message").empty(); // To remove the previous error message
        var file = this.files[0];
        var imagefile = file.type;
        var match = ["image/jpeg","image/png","image/jpg"];
        if(!((imagefile==match[0]) || (imagefile==match[1]) || (imagefile==match[2]))) {
           $('#previewing').attr('src','no-preview.png');
           $("#message").html("<p id='error'>Please Select A valid Image File</p>"+"<h4>Note</h4>"+"<span id='error_message'>Only jpeg, jpg and png Images type allowed</span>");
           return false;
        }
        else
        {
            var reader = new FileReader();
            reader.onload = imageIsLoaded;
            reader.readAsDataURL(this.files[0]);
         }
     });
});

function imageIsLoaded(e) {
    $("#file").css("color","green");
    $('#image_preview').css("display", "block");
    $('#previewing').attr('src', e.target.result);
    $('#previewing').attr('width', '250px');
    $('#previewing').attr('height', '230px');
};
//check nudity function
function onImageClick(node) {
    nude.load(node);
    // Scan it
    nude.scan(function(result){ 
        console.log(result ? "Nudity found in " + node + "!" : "Not nude");
                console.log("returned value is:",result);
                return result;
    });
}
});

编辑:我根据@Amir答案编辑代码,他提到了我以前没有注意到的重要点。
现在,我可以在没有裸体的情况下触发上传函数,但是即使ajax调用中的成功函数被触发,图像也无法上传:

    success: function(data)   
    {
    $('#loading').hide();
    $("#message").html(data);
    }

这是新代码:

$(document).ready(function (e) {
$("#uploadimage").on('submit',(function(e) {
e.preventDefault();
//call check nudity function
onImageClick('previewing');
//check nudity function
function onImageClick(node) {
    nude.load(node);
    // Scan it
    nude.scan(function(result){ 
        console.log(result ? "Nudity found in " + node + "!" : "Not nude");
                console.log("returned value is:",result);
                if(result){
                    alert("conatain nudity!!");
                }
      else{
            $("#message").empty();
            $('#loading').show();
                $.ajax({
            url: "ajax_php_file.php", 
            type: "POST",             
            data: new FormData(this), 
            contentType: false,       
            cache: false,             
            processData:false,        
            success: function(data)   
            {
            $('#loading').hide();
            $("#message").html(data);
            }
                });
              }
    });
};

}));

// Function to preview image after validation
$(function() {
$("#file").change(function() {
$("#message").empty(); // To remove the previous error message
var file = this.files[0];
var imagefile = file.type;
var match= ["image/jpeg","image/png","image/jpg"];
if(!((imagefile==match[0]) || (imagefile==match[1]) || (imagefile==match[2])))
{
$('#previewing').attr('src','no-preview.png');
$("#message").html("<p id='error'>Please Select A valid Image File</p>"+"<h4>Note</h4>"+"<span id='error_message'>Only jpeg, jpg and png Images type allowed</span>");
return false;
}
else
{
var reader = new FileReader();
reader.onload = imageIsLoaded;
reader.readAsDataURL(this.files[0]);
}
});
});
function imageIsLoaded(e) {
$("#file").css("color","green");
$('#image_preview').css("display", "block");
$('#previewing').attr('src', e.target.result);
$('#previewing').attr('width', '250px');
$('#previewing').attr('height', '230px');
};

});

nude.scan是异步的,因此您应该传递回调。 就像是:

nude.scan(function (result) { /* your logic according to the result */ })

我找到了解决方案,问题出在FormData上,并且返回了: TypeError:FormData.constructor的参数1没有实现HTMLFormElement接口。 由于某种原因,它没有出现在chrome控制台中,但是出现在Firefox中。 所以我在else语句中添加了这行代码(以防发现裸体):

var form = $('form').get(0); 

此代码返回一个jQuery对象( $('form') ),并将HTML元素传递给FormData( get(0) )。 然后在ajax请求中: data: new FormData(form),
希望此解决方案可以帮助其他人。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM