jQuery 验证和图像文件

jQuery Validation and Image file

好的,我遇到过将图像文件发布到服务器的情况,但在此之前我会使用 "required" 属性验证用户是否选择了该文件。还使用 java 功能预览图像。现在问题是在编辑模式的情况下,当信息加载到页面时,我显然没有加载图像文件,而是我只是创建预览路径并将其分配给图像路径。所以当我编辑后保存信息时再次问我 "image is required"。系统是正确的,因为我没有选择任何图像。我该如何解决这个问题?我想强制执行 jquery 验证,但在编辑模式的情况下,它应该跳过图像验证步骤。

脚本

<script>
function readURL(input) {
    if (input.files && input.files[0]) {
        var reader = new FileReader();            
        reader.onload = function (e) {
            $('#imgpreview1').attr('src', e.target.result);
        }

        reader.readAsDataURL(input.files[0]);
    }
}

$("#file").change(function () {
    readURL(this);
});


<script/>                    

查看

@Html.Label("Venue Description Background") 
@Html.TextBoxFor(model => model[0].Image.ImageFile, new { type = "file", id="file", required="required"})

<img src="@Model[0].Image.ImagePath" id="imgpreview1"  style="height:150px;width:150px"/>

型号

   public iSPYImage Image { get; set; }

    [DisplayName("Image File")]
    public HttpPostedFileBase ImageFile{ get; set;}

在视图定义中使用一些条件。类似于:

if(editMode)
     @Html.TextBoxFor(model => model[0].Image.ImageFile, new { type = "file",        id="file"})
else
     @Html.TextBoxFor(model => model[0].Image.ImageFile, new { type = "file", id="file", required="required"})