清除文件上传

时间:2014-06-04 07:58:52

标签: c# jquery internet-explorer

我有jQuery函数来验证上传文件的文件扩展名。如果该文件不属于所需的文件扩展名,则会显示验证消息,我将通过jQuery清除File Upload的值

$('#ctl00_ContentPlaceHolder1_FileUpload').val("")

它在现代浏览器中运行良好,但在IE 8中没有。我也尝试过

document.getElementById('ctl00_ContentPlaceHolder1_FileUpload').value = ''

但也没有工作。我的完整代码是

if ($.inArray(ext, ['gif', 'png', 'jpg', 'jpeg', 'bmp']) == -1) {
    alert('Image must be .jpg/.jpeg/.gif/.bmp/.png only.');
    $('#ctl00_ContentPlaceHolder1_FileUpload').val("");
    document.getElementById('ctl00_ContentPlaceHolder1_FileUpload').value = '';
    $("#ctl00_ContentPlaceHolder1_FileUpload").focus();
}

1 个答案:

答案 0 :(得分:0)

IE有{strong> security restriction(read-only) 超过<input type="file">

因此,解决方法是 clone() 并替换当前的<input>

if(navigator.userAgent.toUpperCase().indexOf('MSIE') >= 0){
    $("input[type='file']").replaceWith($("input[type='file']").clone(true));
} else {
    $("input[type='file']").val('');
}

另外,如果表单$('#form')[0].reset();是表单中唯一的字段,则表单{{1}}可以是一个选项。

相关问题