我有一个问题,如果文件扩展名不正确,它会显示警报。这很好用,但问题是它不应该上传文件。为什么要上传文件,因为在下面的函数中我已经说明如果通过了 imageValidation,则转到“startUploadImage()”函数开始上传:
function imageClickHandler(imageuploadform){
if(imageValidation(imageuploadform)){
return startImageUpload(imageuploadform);
}
return false;
}
下面是检查文件扩展名的 imageValidation() 代码:
function imageValidation(imageuploadform) {
var val = $(imageuploadform).find(".fileImage").val();
switch(val.substring(val.lastIndexOf('.') + 1).toLowerCase()){
case 'gif':
case 'jpg':
case 'jpeg':
case 'pjpeg':
case 'png':
return true;
case '':
$(imageuploadform).find(".fileImage").val();
alert("To upload an image, please select an Image File");
return false;
default:
alert("To upload an image, please select a valild file extension.");
return false;
}
return false;
}
我不知道你是否需要查看 startImageUpload 函数,但如果你这样做了,那么这也在下面:
function startImageUpload(imageuploadform){
$(imageuploadform).find('.imagef1_upload_process').css('visibility','visible');
$(imageuploadform).find('.imagef1_cancel').css('visibility','visible');
$(imageuploadform).find('.imagef1_upload_form').css('visibility','hidden');
sourceImageForm = imageuploadform;
return true;
}
显示表单的代码:
var $fileImage = $("<form action='imageupload.php' method='post' enctype='multipart/form-data' target='upload_target' onsubmit='imageClickHandler(this);' class='imageuploadform' >" +
"Image File: <input name='fileImage' type='file' class='fileImage' /></label><br/><label>" +
"<input type='submit' name='submitImageBtn' class='sbtnimage' value='Upload' /></label>" +
"</p><p class='imagef1_cancel' align='center'><label>" +
"<input type='button' name='cancelImageBtn' class='cancelimage' value='Cancel' /></label>" +
"</p></form>");