【发布时间】:2015-10-06 02:59:32
【问题描述】:
我正在制作一个图片上传表单,该表单将在nudejs 脚本的帮助下验证上传的图片是否包含裸露内容。
如果图片不包含裸露内容,则会上传。
上传和检查裸露功能运行良好,但问题是自从检查裸露功能“onImageClick()”返回'undefined' 后,我没有找到链接这两种方法的方法,所以我无法检查如果它的真假,以便通过$.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');
};
});
【问题讨论】:
-
好笑,我以为你想说nodejs,但你是对的
-
是的,nude.js :) 如果您对如何解决这个问题有任何想法,我将不胜感激 :)
-
无论结果如何,请记住您正在浏览器上进行此验证。恶意用户总是有可能绕过浏览器端验证。如果您需要对此采取防弹措施,那么您还需要在上传图片后在服务器上进行验证。
-
@Spudley 感谢您提供此信息,我如何在服务器端也检查此信息?你的意思是中等功能还是自动脚本?
-
它可以是适度的或自动的,这取决于您的需要。我不知道有任何用于服务器端任务的自动化工具,但我确信它们一定存在。
标签: javascript jquery ajax nude.js