Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如果图像不是';无效_Javascript_Jquery_Asp.net Mvc - Fatal编程技术网

Javascript 如果图像不是';无效

Javascript 如果图像不是';无效,javascript,jquery,asp.net-mvc,Javascript,Jquery,Asp.net Mvc,如果文件不是jpg、jpeg等,我想将文件输入元素设置为null,但当我将保存HttpPostedFileBase的元素的值设置为null时,它会重新触发更改事件并创建错误。有没有办法绕过这个问题 我想我可以在更改事件开始时检查该值是否为null,但它不起作用 这是我的html元素(有两个,因为我隐藏了upload文本框并将其显示为按钮) 所以我想我明白了! 如果文件不是jpg/gif/etc,我在下面添加了一行 default: alert('Unsup

如果文件不是jpg、jpeg等,我想将文件输入元素设置为null,但当我将保存HttpPostedFileBase的元素的值设置为null时,它会重新触发更改事件并创建错误。有没有办法绕过这个问题

我想我可以在更改事件开始时检查该值是否为null,但它不起作用

这是我的html元素(有两个,因为我隐藏了upload文本框并将其显示为按钮)


所以我想我明白了! 如果文件不是jpg/gif/etc,我在下面添加了一行

        default:
            alert('Unsupported File!');
            $("#selectedFile").replaceWith($("#selectedFile").clone(true));
            return false;
它会清除httppostedfilebase memeber,因此当我向服务器发回帖子时,该值为null。也许这不是一个好的解决方案,但这是我迄今为止所发现的全部。仅仅通过设置元素的值来清除元素,就会重新触发更改事件,这正是我试图避免的。除非有一些简单的逻辑,我可以把它放进改变事件中去对抗它

$(function () {
console.log("ready!");
alert("picture input function entered");

$("#pictureupload").click(function() {
    document.getElementById('selectedFile').click();
});

$("#selectedFile").change(function() {
    //this doesn't work 
    var imgVal = $('selectedFile').val();
    if (imgVal == '')
        return false;

    //check whether browser fully supports all File API
    if (window.File && window.FileReader && window.FileList && window.Blob) {
        //get the file size and file type from file input field
        var fsize = $('#selectedFile')[0].files[0].size;
        var ftype = $('#selectedFile')[0].files[0].type;
        var fname = $('#selectedFile')[0].files[0].name;

        switch (ftype) {
            case 'image/png':
            case 'image/gif':
            case 'image/jpeg':
            case 'image/pjpeg':
                alert("Acceptable image file!");

                break;
            default:
                alert('Unsupported File!');
                $('#selectedFile').val(null); //here is where the onchange gets triggered again and 
                return false;
        }

        var oFReader = new FileReader();
        oFReader.readAsDataURL(document.getElementById("selectedFile").files[0]);

        oFReader.onload = function (oFREvent) {
            document.getElementById("uploadPreview").src = oFREvent.target.result;
        };

    } else {
        alert("Please upgrade your browser, because your current browser lacks some new features we need!");
        return false;
    }
});
});
        default:
            alert('Unsupported File!');
            $("#selectedFile").replaceWith($("#selectedFile").clone(true));
            return false;