gpt4 book ai didi

javascript - 如果给定的文件格式错误,则重置输入文件

转载 作者:行者123 更新时间:2023-12-03 05:12:35 25 4
gpt4 key购买 nike

如果文件格式不是图像格式,如何将输入文件设置回“未选择文件”?

<input type="file" name="image" id="image" value="" class="form-control" />

<script>
$("#image").change(function()
{
var Data = document.getElementById('image');
var FileUploadPath = Data.value;

if (FileUploadPath != '') {
var Extension = FileUploadPath.substring(FileUploadPath.lastIndexOf('.') + 1).toLowerCase();
//The file uploaded is an image

if (Extension == "gif" || Extension == "png" || Extension == "bmp"
|| Extension == "jpeg" || Extension == "jpg"){
return true;
}else if (Extension != 'gif' || Extension != 'png' || Extension != 'bmp' || Extension != 'jpeg' || Extension != 'jpg') {

alert("Photo only allows file types of GIF, PNG, JPG, JPEG and BMP. ");
return false;
}
}
});
</script>

最佳答案

$("#image").change(function()
{
var Data = document.getElementById('image');
var FileUploadPath = Data.value;

if (FileUploadPath != '') {
var Extension = FileUploadPath.substring(FileUploadPath.lastIndexOf('.') + 1).toLowerCase();
//The file uploaded is an image

if (Extension == "gif" || Extension == "png" || Extension == "bmp"
|| Extension == "jpeg" || Extension == "jpg"){
return true;
}else if (Extension != 'gif' || Extension != 'png' || Extension != 'bmp' || Extension != 'jpeg' || Extension != 'jpg') {

alert("Photo only allows file types of GIF, PNG, JPG, JPEG and BMP. ");
Data.type = '';
Data.type = 'file';
return false;
}
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="file" name="image" id="image" value="" class="form-control" />

关于javascript - 如果给定的文件格式错误,则重置输入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41744332/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com