为什么它不应该上传图像

why is it uploading image when it shouldn't do?

本文关键字:图像 不应该 为什么      更新时间:2023-09-26

>我有一个问题,如果文件扩展名不正确,它会显示警报。这很好用,但问题是它不应该上传文件。为什么要上传文件,因为在下面的函数中我已经说过,如果通过 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>");

问题出在表单提交事件中。你应该看起来像这样:

onsubmit="return imageClickHandler(this);"

请注意返回。如果不存在,则不会返回图像处理程序函数的结果,并且不会取消提交事件。

此外,实际上在属性上使用单个 qoutes (') 并不是真正有效的 html。您可能应该将它们切换到双 qoutes,然后使用单 qoutes 作为 javascript 字符串的终止符。喜欢这个:

var $fileImage = $('<form action="imageupload.php" method="post" enctype="multipart/form-data" target="upload_target" onsubmit="return 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>');

看起来您的目标图像上传表单不正确

如果是 id,则应为 $("#imageuploadform"),如果是类,则应为 $(".imageuploadform")

但似乎coe可能还有更多问题