它为什么不上传图像?

时间:2012-04-14 21:41:11

标签: javascript jquery

我遇到一个问题,如果文件扩展名不正确,则会显示警告。这会产生罚款,但问题是它不应该上传文件。为什么要上传文件,因为在下面的函数中我已经声明如果传递了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>");

2 个答案:

答案 0 :(得分:1)

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

onsubmit="return imageClickHandler(this);"

请注意返回。如果不存在,则不返回imagehandler函数的结果,并且不会取消提交事件。

另外,实际上在属性上使用单个qoutes(')并不是真正有效的html。您应该将它们切换为double 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>');

答案 1 :(得分:0)

看起来你的目标图像上传不正确

如果它是id或$(“。imageuploadform”)它应该是$(“#imageuploadform”),如果它是一个类

但似乎这个系列可能还有更多错误